fork download
  1.  
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5.  
  6. bool recognize_string(char *string)
  7. {
  8. int len = strlen(string);
  9. int i = 0;
  10. while (string[i] == 'a')
  11. {
  12. i++;
  13. }
  14. if (i == len)
  15. {
  16. return false;
  17. }
  18. while (string[i] == 'b')
  19. {
  20. i++;
  21. }
  22. if (i == len)
  23. {
  24. return true;
  25. }
  26. return false;
  27. }
  28.  
  29. int main()
  30. {
  31. char string[100];
  32. printf("Enter a string: ");
  33. scanf("%s", string);
  34. if (recognize_string(string))
  35. {
  36. printf("%s matches the rule a^*b^+\n", string);
  37. }
  38. else
  39. {
  40. printf("%s does not match the rule a^*b^+\n", string);
  41. }
  42. return 0;
  43. }
Success #stdin #stdout 0s 5532KB
stdin
Standard input is empty
stdout
Enter a string:  does not match the rule a^*b^+