Untitled
unknown
plain_text
a month ago
1.1 kB
3
Indexable
Never
#include <stdio.h> #include <string.h> // Function to check if the string follows the grammar int checkGrammar(char *str) { int len = strlen(str); // If the string is too short, it can't match the grammar if (len < 2) { return 0; // Not valid } // Check if the first character is 'a' and the last is 'b' if (str[0] != 'a' || str[len - 1] != 'b') { return 0; // Not valid } // Check all characters in between for (int i = 1; i < len - 1; i++) { if (str[i] != 'a' && str[i] != 'b') { return 0; // Not valid } } // If all checks pass, the string is valid return 1; // Valid } int main() { char str[100]; // Get the input string printf("Enter a string: "); scanf("%s", str); // Check the string against the grammar if (checkGrammar(str)) { printf("The string is valid under the given grammar.\n"); } else { printf("The string is NOT valid under the given grammar.\n"); } return 0; }
Leave a Comment