Back to DFS's C Page


OAC Problem Set II

Strings
Due 25 Oct 99

This simple exercise is designed for you to learn how to use the string functions which are made accessible by including the following compiler directive in your programs.

#include <string.h>

Your program is to do the following:

  1. Declare and initialize a variable called ethnicgroup with the string "Slovenian" using the following:

       char ethnicgroup[15] = "Slovenian";
    

    Only when declaring a string variable are you permitted to use the assignment operator to place a string in a variable. Otherwise, any time you wish to assign more than a single character in a simple statement, you must use a function, either predefined or created by you.

  2. Declare the string variables plural, trait, and country with enough bytes so that they can be used to store strings of up to twenty bytes in length. Remember: The null character which is used to terminate every string must be included in the count.

  3. Use strlen() to determine and save the length of ethnicgroup in a variable. Use this int variable and ethnicgroup to report the length in a grammatical sentence.

  4. Use a loop to copy the contents of ethnicgroup into plural and use a function to concatenate an s on the end of plural. Then report that this is the plural of ethnicgroup, using the two variables to produce a complete sentence.

  5. Use a function to copy the 2nd through the 5th characters (from the human perspective) stored in ethnicgroup into trait. Then use these two variables to report that the data in the latter was found in the former.

  6. Use a function to copy the contents of ethnicgroup into country and then replace the last character (using a function call as part of the subscript) with the NULL character. Then report in a sentence that the people known by the word in plural live in the country whose name is stored in country.

  7. Use strcmp() to compare the words in ethnicgroup and country, saving the return value in a variable. Then, using a double-option if statement in the most efficient way possible, report the value of this variable and state which word appears first in alphabetical order.


Created 17 Oct 99
Modified 25 Aug 05
© DFStermole 1999-2005