// Lecture Example recap strings #include #include #define MAX_LEN 100 void remove_trailing_nl(char s[]); int all_lowercase(char s[]); void reverse(char s[MAX_LEN], char reverse_s[MAX_LEN]); int main(void) { char str[MAX_LEN]; char reverse_s[MAX_LEN]; printf("Enter a string: "); while (fgets(str, MAX_LEN, stdin) != NULL) { remove_trailing_nl(str); printf("You typed in %s\n", str); if (strcmp("cake", str) == 0) { printf("hooray there is cake\n"); } if (all_lowercase(str) == 1) { printf("You have all lower case characters\n"); } reverse(str, reverse_s); printf("%s\n", reverse_s); printf("Enter a string: "); } return 0; } // Remove the trailing newline character of the string // if it exists void remove_trailing_nl(char s[]) { int len = strlen(s); if (s[len - 1] == '\n') { s[len - 1] = '\0'; } } // returns 1 if all characters (except '\0') are lowercase // returns 0 otherwise int all_lowercase(char s[]) { int all_lower = 1; for (int i = 0; s[i] != '\0'; i++) { if (s[i] < 'a' || s[i] > 'z') { all_lower = 0; } } return all_lower; } // Make a reverse copy of the string in the s array input // and put the reverse copy in the reverse_s array input // Note: We need to make sure that reverse_s is big enough void reverse(char s[MAX_LEN], char reverse_s[MAX_LEN]) { int j = strlen(s) - 1; for (int i = 0; s[i] != '\0'; i++) { reverse_s[j] = s[i]; j--; } reverse_s[strlen(s)] = '\0'; }