CSC 161 | Grinnell College | Spring, 2009 |
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.
This lab assumes that you have access to a C programming language reference, particularly for the C string function library. If you do not have a published reference in paper form, you might consider the following on-line reference:
Write a short program that repeatedly prompts the user to enter a character and prints that character to the console. For this exercise, use the function getchar to read characters. The repetition should stop once the user has entered the character 'q'.
Does your program behave as you expected? Consider that when you enter a character you must also press Enter before the character is available to your program. At that point, both characters are in the "input buffer". Since your program reads characters one at a time, it will process the desired character, then process the '\n', and then wait for further input from the user.
Modify your program so that after processing the desired character, it reads and "discards" any additional characters up to and including the '\n'. (In other words, read the characters but don't do anything with them. This removes them from the input buffer and effectively discards them.) The program should then behave as expected.
Similar results (and a similar lesson) can be obtained when using scanf to read characters. Modify your program to read characters using
scanf("%c", &ch)
Then modify it one last time to read with
scanf(" %c", &ch)
(A careful inspection will reveal an extra space in the format string of the second function call. Ordinarily, scanf skips leading whitespace except with the format code "%c"; however, adding a space before "%c" specifically requests that leading whitespace be skipped.
Copy and run the two programs from today's reading.
For each program, write a paragraph to explain how the code works.
As you know, strings are arrays of char variables. We could write:
char myString[] = { 'H', 'e', 'l', 'l', 'o'};But there is a specialized syntax for initializing strings:
char yourString[] = "Hello";
Write a program that contains the "declaration-initializations" given above. Then use printf, with the format code %s to print each string.
Did you get the result you expected? What is different between the two examples?
Program string-example.c illustrates several elements of strings in C, including forms of declarations and the string functions strcpy and strstr:
The first several exercises ask you to review the workings of these functions in typical circumstances.
Copy ~walker/c/strings/string-example.c to your account, compile it, and run it, and observe the output.
Explain the initial values printed. Why do several strings have the same values? For example, what is the effect of the line a = s?
Just before the printing of initial values, add the line
s = a;
What happens when you try to compile and run the program? Explain the result. (Restore the original program before continuing with the lab.)
Review the values after modifications, and explain each printed result. (For example, why is the letter 'r' found in strings s and a?)
The printout for Searching results first indicates the base address of the e array. Note that the format %u in the printf statement prints this address as an unsigned integer.
Using the base address of e as the starting point, explain the results printed for the index of 4, e, and X.
In the program, replace the line a[5]='p' by the line a[5]=0. That is, replace 'p' by the number 0. Recompile and rerun the program, and explain the output obtained.
Write a function that accepts a string argument and returns the number of characters (before the null terminator) in the string. Your function header could look like this:
int myStrlen(const char str[])
Note that "const-qualifying" the parameter as shown here ensures that the function will not modify the string. (If it tries to, the code will not compile.) Thus, it is a simple way to add a safety feature.
To test your code, add a main function that repeatedly prompts for and accepts words typed at the keyboard. For each word the user types, display the length of the word. Keep processing words until the user types "quit".
You may want to read the words with something like the following, assuming that you have declared str with sufficient size to hold 20 characters plus the null character.
scanf("%20s", str);
Recall that you should not use an ampersand before the string name since the name of an array already evaluates to the address of element 0 (the first character in the string).
Write a function propercase(str) that changes the first character of the input string to uppercase and changes the remaining characters to lowercase.
Your function should use the following header.
void propercase(char str[])
Notice that the string argument is intentionally not const-qualified. This allows the function to modify the original string, which was declared in the calling function.
If you want to use the C library functions toupper(ch) and tolower(ch), be sure to include ctype.h.
Modify your main function to test propercase(str).
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/161.sp09/lab-strings.shtml
Parts A, B, D: | created Fall 2001 for CSC 180 at the University of Toronto
by Alan Rosenthal revised Fall 2002 by Alan Rosenthal (Toronto) revised Fall 2003, Fall 2004 by Tom Fairgrieve (Toronto) revised Fall 2004, Fall 2005 by Marge Coahran (Toronto) revised January 2007 by Marge Coahran (Grinnell) with permission from Alan and Tom revised February 2007, February 2008 by Marge Coahran |
![]() ![]() |
Part C: | created 21 January 2005 by Henry M. Walker revised 11 April 2008 |
|
Full Lab: | revised 13 May 2008 by Henry M. Walker last revised 22 March 2009 by Henry M.Walker | |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |