1.FSM
Mon Apr 07 2025 13:39:12 GMT+0000 (Coordinated Universal Time)
Saved by
@sem
#include <stdio.h>
#include <string.h>
int hasEqualAB(const char* input) {
int count = 0;
for (int i = 0; input[i] != '\0'; i++) {
if (input[i] == 'a') {
count++;
} else if (input[i] == 'b') {
count--;
} else {
// Invalid character
return 0;
}
}
return count == 0;
}
int main() {
char str[100];
printf("Enter a string (only a and b): ");
scanf("%s", str);
if (hasEqualAB(str)) {
printf("Accepted: Equal number of 'a' and 'b'\n");
} else {
printf("Rejected: Unequal number of 'a' and 'b'\n");
}
return 0;
}
content_copyCOPY
Comments