Previous: 11.6 Exercises
Up: 11 String Processing
Previous Page: 11.6 Exercises

11.7 Problems

Write program drivers for each of the following. The driver should read appropriate data until end of file, call the functions described below, and print the results.

  1. Write a function that returns the index where a character, c, occurs in a string, s. The function returns -1 if c is not present in s. Use array indexing.

  2. Repeat 1 using pointers.

  3. Write a function that returns the index where a character, c, occurs in a string s; the search for c starting at a specified index, i in s. The function returns -1 if c is not present in s starting at the index, i. Use array indexing.

  4. Repeat 3 using pointers.

  5. Write a function, how_many(), that returns the number of times a character, ch, occurs in a string, s. Use array indexing.

  6. Repeat 5 using pointers.

  7. Write a function that substitutes a new character, newc, for the first occurrence of a character, c, in a string, s. Use array indexing.

  8. Repeat 7 using pointers.

  9. Write a function that substitutes a new character, newc, for every occurrence of a character, c, in a string, s. Use array indexing.

  10. Repeat 9 using pointers.

  11. Rewrite the function, our_strcpy() in Section 11.2.2 so that it properly returns the pointer to the destination string.

  12. Write a function that takes two strings, s and t, as arguments. Copy string s into t, but remove all white space and punctuation. Use array indexing.

  13. Repeat 12, but use pointers.

  14. Write a function that takes a string of characters and removes all white space and punctuation in that same string. Use array indexing.

  15. Repeat 14 using pointers.

  16. Write a function, xwslead(), that removes all leading white space from a string. Use array indexing.

  17. Repeat 16 using pointers.

  18. Write a function, xwstrail(), that removes all trailing white space from a string. Use array indexing.

  19. Repeat 18 using pointers.

  20. Write a function, xws(), that removes all leading and trailing white space from a string. Use array indexing.

  21. Repeat 20 using pointers.

  22. Write a function, squeeze(), that removes all white space from a string. Use array indexing.

  23. Repeat 22 using pointers.

  24. Write a function, compare(), that takes two strings as arguments and compares them for equality after leading and trailing blanks are removed. If the strings are equal after the leading and trailing blanks are removed, the function returns True. Otherwise, it returns False.

  25. Write a function, compstrip(), that takes two strings as arguments and compares stripped versions of them. A stripped string is one from which all white space and punctuation are removed. Function returns True if the strings are equal after they are stripped.

  26. Write a function, palindrome() that checks if a given string is the same forwards and backwards. Use pointers.

  27. Write a function that checks if a string is a palindrome ignoring all white space. Example:

    i ia wah hawaii

  28. Write a function that takes two string arguments, s and t. Copy s into t in reverse order, except that a sequence of white space is squeezed to a single space.

  29. Write a function that takes a single string argument, and reverses the string itself, except that white space is squeezed to a single space.

  30. Write a function that removes the first word from a string. Write a program that uses the function to remove a specified number of leading words from a string.

  31. Write a function that removes the last word in a string. Write a program that uses the function to remove a specified number of trailing words from a string.

  32. Write a function that takes two strings, s1 and s2 as arguments. It returns the index where s2 occurs in s1, or it returns -1 if s2 is not in s1.

  33. Write a function that substitutes a new string, repl_str, for the first occurrence of a string, str in a string, src.

  34. Write a function that replaces a new string, repl_str, for every occurrence of a string, str, in src.

  35. Write a function that detects the presence of a whole word, wd, in a string, s.

  36. Write a function that converts a string into an integer. The conversion is terminated when a non-digit is encountered.

  37. Write a function that converts a string into a float. The conversion is terminated when a character that does not belong in a decimal number is encountered.

  38. Write a function that converts an integer to a string.

  39. Write a function that converts a float to a string.

  40. Write a function that converts a string of binary digits to an integer.

  41. Write a function that converts an unsigned integer into a string of binary digits.

  42. Write a function, nexttok(), that gets the next token from a string, starting at a specified array index, called cursor. The function returns the new value of cursor, the token itself, and the type of the token. Leading white space is skipped. A longest valid token is built as long as the characters belong to a token type. The token is complete when a character that does not belong to a token type being built is encountered.

    A valid token type is either an identifier, an integer, a float, an invalid, or an EOS, end of string. An identifier starts with a letter, and may be followed by letters and/or digits. An integer starts with a digit, and may be followed by digits. A float must start with a digit, may be followed by digits, may be followed by a decimal point, and may be followed by a sequence of digits. A character other than white space, letters, and digits is an invalid type token containing that one character. EOS type of token is returned when the NULL character is reached.

    Write a program that reads in lines of input from a file, and use the above function to print out the tokens in each line until EOF.



Previous: 11.6 Exercises
Up: 11 String Processing
Previous Page: 11.6 Exercises

tep@wiliki.eng.hawaii.edu
Sat Sep 3 07:04:57 HST 1994