Programming Exercises

I l @ ve RuBoard

Programming Exercises

  1. Design a function that fetches the next n characters from input, including blanks, tabs, and newlines.

  2. Modify the function in Exercise 1 so that it stops after n characters or after the first blank, tab, or newline, whichever comes first. (Don't just use scanf() .)

  3. Design a function that fetches the first word from a line of input and discards the rest of the line. Define a word as a sequence of characters with no blanks, tabs, or newlines in it.

  4. Design a function that searches the specified string for the first occurrence of a specified character. Have the function return a pointer to the character if successful, and a null if the character is not found in the string. (This duplicates the way that the library strchr () function works.)

  5. Write a function is_within() that takes a character and a string pointer as arguments. Have the function return a nonzero value (true) if the character is in the string and zero (false) otherwise .

  6. The strncpy (s1,s2,n) function copies exactly n characters from s2 to s1 , truncating s2 or padding it with extra null characters as necessary. The target string may not be null- terminated if the length of s2 is n or more. The function returns s1 . Write your own version of this function.

  7. Write a function string_in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string_in("hats", "at") would return the address of the a in hats . Otherwise, have the function return the null pointer.

  8. Write a function that replaces the contents of a string with the string reversed .

  9. Write a function that takes a string as an argument and which removes the spaces from the string. Test in a program that uses a loop to read lines until you enter an empty line. The program should apply the function to each input string and display the result.

  10. Write a program that reads in up to 10 strings or to EOF , whichever comes first. Have it offer the user a menu with five choices: print the original list of strings, print the strings in ASCII collating sequence, print the strings in order of increasing length, print the strings in order of the length of the first word in the string, and quit. Have the menu recycle until the user enters the quit request. The program, of course, should actually perform the promised tasks .

  11. Write a program that reads input up to EOF and reports the number of words, the number of uppercase letters, the number of lowercase letters , the number of punctuation characters, and the number of digits. Use the ctype .h family of functions.

  12. Write a program that echoes the command-line arguments in reverse word order. That is, if the command-line arguments are see you later , the program should print later you see .

  13. Write a power-law program that works on a command-line basis. The first command-line argument should be the type double number to be raised to a power, and the second argument should be the integer power.

  14. Use the character classification functions to prepare an implementation of atoi() .

  15. Write a program that reads input until end of file and echoes it to the display. Have the program recognize and implement the following command-line arguments:

     -p  Print input as is -u  Map input to all uppercase -l  Map input to all lowercase 
I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net