#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; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter