CSC 161 Grinnell College Fall, 2011
 
Imperative Problem Solving and Data Structures
 
 

Laboratory Exercise on Characters and Strings

Goals

This laboratory exercise examines characters, details of string storage, and the operations of string library functions within the C programming language.

Declaring Strings

  1. Here are a number of different string declerations.
    char *baboon;
    char *chimpanzee = "animal";
    char dolphin[];
    char emu[] = "animal";
    char fox[4] = "animal";
    char giraffe[8] = "animal";
    char elephant[10];
    elephant = "animal";
    

    Which are valid and which are invalid?

  2. How do the valid declarations differ?
  3. What happens if you switch fox and giraffe? How do you think this can be explained? Think about the bounds of arrays.

Importance of the Null Character

Since strings are also arrays of char variables, you could write:

char stringa[] = {'H', 'e', 'l', 'l', 'o', '\0'};

You could also write it in this way:

char stringb[] = "Hello";

Both of them have a null('\0') character at the end of the string. The difference is that the null character has to be explicitly there in the first definition. However, in the second definition, the compiler does it for you.

  1. Now take the program string-intro.c . Compile and run the program to see what it does. Why is stringa behaving the way it is?

  2. Now fix the program to have the same string printed out for both initialization types.

Initialized and Uninitialized Strings

  1. Copy the following declaration and code into a main menthod making sure that you include the library string.h:

    char computerscience[16] = "isawesome";
    char isawesome[16] = "computerscience";
    
    printf ("sizeof (computerscience): %d\n", sizeof (computerscience) );
    printf ("sizeof (isawesome): %d\n", sizeof (isawesome) );
    
    printf ("strlen (computerscience): %d\n", strlen (computerscience) );
    printf ("strlen (isawesome): %d\n", strlen (isawesome) );
    
    printf ("computerscience: %s\n", computerscience );
    printf ("isawesome: %s\n", isawesome );
    
    
  2. What would you expect to get if you had written:

    char computerscience[16];

    instead of:

    char computerscience[16] = "isawesome";
  3. Add this line of code:
    printf ("Concatenate the strings: %s", strcat (isawesome, computerscience));
    • What is the result? Is this what you expected? Change the bounds of array isawesome to 32 and see what happens? What happened now?
    • What did the string operator strcat() do? Explain conceptually what happens in the array and where the null character(s) is/are?

Characters as Integers

From the reading you have done on Characters and Strings, you have read that char is actually considered to be a type of integer. We can use this property of chars to do integer arithmetic on their values. Every char has a corresponding integer. As you know, we can find out what these values are from an ASCII table.

  1. Write a program that will take the given hardcoded character, and print out the character and its corresponding integer value in this example format:

     'A' = 65 

    Hint: Since characters are integers, you need to only use %d to print the integer.

  2. Write a program that takes the name of the robot by using rGetName(), converts all the characters of the name to uppercase (without using isupper), and then sets the robot's name to the uppercase version, using rSetName().

    Keep in mind that the value of a character is represented by putting single quotes, and string is represented by double quotes.

Control Characters

Characters can represent actions rather than just printing a symbol. Here is a short list of what can be done with some characters:

  1. Write a program that:
    • prints out a sentence with tabs in between each word
    • prints out a sentence with vertical tabs between each word
    • illustrates how backspace works (print a word with a few backspace and see how much of the word you can read)

String Functions

  1. Write a function with the following prototype;
    void string_reverse (char str[]):

    It should reverse the order of the characters in str (except the null character). Note that it will not return a new string, but it will modify the given string.

Robots with Strings

  1. Write a program that beeps the number of letters there are in a word. Don't use the string operator strlen().

String Operators

Remember from yesterday's reading that:

  1. Write a program that uses string operators to put given, hardcoded, strings into order.

    Note: Put the strings in an arrays of strings and use a temporary place to swap the strings.

Strings of Music

  1. Write a program that makes the robot beep in the frequecies that corresponds to the musical letters (A,B,C,D,E,F,G - ignore sharps and flats- and H!) that are given in a string. Make the program so that it is not case-sensitive. Here is the header file to define pitches and their corresponding letters: pitches.h Remember that a string is actually an array.

    For example, give it the word "BED" and play the frequencies for B, E, and D.

  2. Now make this program work for all the letters in the alphabet. Hint: Use mod to wrap the letters back to the musical letters. For example 'H' would wrap to be 'A' ,'I' would wrap to be 'B', 'J' would wrap to be 'C', and so on.

Reminder: Complete Evaluation Form

When you have finished this lab, be sure to fill out its evaluation form in the "Lab Evaluation" section for CSC 161 on Pioneer Web.