EXERCISES ON STRINGS READING ------- Sedgewick, 108-114 Kernighan and Ritchie, 1.5-6, 5.5 EXERCISES --------- 1. Which of the following sets i to the length of the string a? A. i = strlen(a); B. i = 0; while (a[i] != '\0') ++i; C. i = 0; while (a[i] != '\0') i++; D. for (i = 0, b = a; *b != '\0'; b++) i++; E. for (i = 0; a[i] != '\0'; i++) ; F. b = a; while (*b++) ; i = b-a-1; G. b = a; while (*b) b++; i = b-a; 2. What is wrong with the following program for printing out the length of a word from standard input? #include #include main() { int i; char a[100]; scanf("%100s", a); for (i = 0; *a != '\0'; a++) i++; printf("%2d %s\n", i, a); } 3. Explain what the following program does. #include #include main() { int i, N; char word[100]; while (scanf("%s", word) != EOF) { N = strlen(word); for (i = 0; i < N; i++) if (word[i] <= word[i+1]) break; if ((i > 4) && (i == N)) printf("%2d %s\n", N, word); } } 4. The program given in exercise 3 has a serious bug that is commonly found in C programs. Find the bug and explain how to fix it. 5. Write a program that prints out any words in its input that are palindromes (read the same forwards and backwards. 6. Why not use the following in as the for loop in exercise 3? for (i = 0; i < strlen(word); i++) 7. Write a C program that takes an integer and a file name as input and prints out all instances in the file where the number of consecutive occurrences of some character is greater than the given integer. . . ANSWERS TO EXERCISES ON STRINGS 1. They all do. A. string library function, K+R page 250 B. K+R page 39 C. i isn't used until after the statement anyway D. K+R page 99 E. Sedgewick, page 114 F. Sedgewick, page 114 G. K+R page 103 2. Can't increment array name. Have to copy into pointer as in 1.D. 3. Prints out all the words in the standard input that have more than four characters and whose characters appear in decreasing order. % echo I tried but I was wrong | a.out 5 tried 5 wrong % cat /usr/dict/words | a.out | grep 6 6 sponge 4. If there is a word with more than 100 letters in the input, scanf will go beyond the bounds of the 100-character array, with unpredictable consequences. The famous Internet worm attack by Robert Morris in 1989 exploited a bug like this in the implementation of the "finger" command. Fix: "%s" -> "%100s". 5. Replace the for loop in the program of exercise 3 with for (i = 0; i < N; i++) if (word[i] != word[N-1-i]) break; This solution is twice as slow as it could be: we could stop the for loop in the middle. Exercise: fix the program to do so. % echo ifihadahifi | a.out 11 ifihadahifi % cat /usr/dict/words | a.out 5 civic 5 level 5 madam 5 minim 5 radar 5 refer 5 rever 5 rotor 5 tenet 6. Calls strlen() every time through the loop. Takes time proportional to the square of the length of the string. There are plenty of C programs out there that are extremely slow for long strings because of this bug. 7. main(int argc, char *argv[]) { int i, j, c, prev; int max = atoi(*++argv); FILE *text = fopen(*++argv, "r"); for (i = 0, j = 0; (c = getc(text)) != EOF; i++, j++, prev = c) if ( c != prev ) { if ( j > max ) printf ("%2d %c's at %d\n", j, prev, i - j); j = 0; } }