| CSC 161 | Grinnell College | Spring, 2012 |
| Imperative Problem Solving and Data Structures | ||
This laboratory exercise examines characters, details of string storage, and the operations of string library functions within the C programming language.
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?
Since strings are also arrays of char variables, you could write:
char stringa[] = {'H', 'e', 'l', 'l', 'o', '\0'};Here the compiler allocates 6 characters for stringa — one for each char in the array.
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.
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?
Now fix the program to have the same string printed out for both initialization types.
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 ("strlen (computerscience): %d\n", strlen (computerscience) );
printf ("strlen (isawesome): %d\n", strlen (isawesome) );
printf ("computerscience: %s\n", computerscience );
printf ("isawesome: %s\n", isawesome );
What would you expect to get if you had written:
char computerscience[16];
instead of:
char computerscience[16] = "isawesome";
printf ("Concatenate the strings: %s", strcat (isawesome, computerscience));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.
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.
Keep in mind that the value of a character is represented by putting single quotes, and string is represented by double quotes.
Characters can represent actions rather than just printing a symbol. Here is a short list of what can be done with some characters:
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.
Write a program that beeps the number of letters there are in a word. Don't use the string operator strlen().
Remember from yesterday's reading that:
A very useful tool to be aware of is the fact that you can man the standard C functions.
Remember that to quit the man pages, you can simple type q.
What does strcat do? Using what you have learned about how strings are stored, and their null characters, explain how strcat works.
Note: Put the strings in an arrays of strings and use a temporary place to swap the strings.
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.
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.
Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.