CSC161 2011S Imperative Problem Solving

Laboratory: Strings in C

Summary: In this laboratory, you will experiment with C's strings and the various standard string procedures.

Contents:

Preparation

Create a new directory for this laboratory. I'd suggest calling it Labs/Strings.

Exercises

Exercise 1: Printing Strings

Suppose we've defined the string antelope as follows.

  char *antelope = "...";

Consider the following two mechanisms for printing the string antelope.

a. Which do you prefer? Why?

b. Write a simple program in which the two statements have different output. How can you get different output? Well, the first parameter to printf is officially a pattern string, so you should think about including a pattern in the string.

Exercise 2: Declaring Strings

Here are a number of different string declarations.

  char *baboon;
  char *chimpanzee = "animal";
  char dolphin[];
  char emu[] = "animal";
  char fox[8];
  char fox[4] = "animal";
  char fox[8] = "animal";

a. Which are valid and which are invalid?

b. How do the valid declarations differ?

Exercise 3: Initialized and Uninitialized Strings

Note that the sizeof function tells you how much memory has been allocated to something wile the strlen function tells you the length of a string (the number of characters before you hit the null terminator).

a. Consider the following declaration and code

  char gorilla[16] = "magilla";
  char ch;
  ...
  printf ("sizeof (gorilla): %d\n", sizeof (gorilla));
  printf ("strlen (gorilla): %d\n", strlen (gorilla));
  printf ("gorilla: '%s'\n", gorilla);

What do you expect the output to be?

b. Check your answer experimentally.

c. How would you expect the output to change if we changed the declaration to

  char gorilla[16];

d. Check you answer experimentally.

e. How would you expect the output to change if we changed the declaration to

  char *gorilla = "magilla";

f. Check you answer experimentally.

g. How would you expect the output to change if we changed the declaration to

  char *gorilla;

h. Check you answer experimentally.

i. What do your results suggest?

Exercise 4: Assigning Strings

Recall that we have two ways to declare strings: as arrays of characters or as pointers to characters.

a. Suppose hippo and iguana are both declared as strings. When, if ever, is

  iguana = hippo;

a valid assignment?

Note that you may need to try different kinds of declarations. (E.g., both could be declared as arrays of characters, both could be declared as pointers to characters, or each could be declared differently.)

b. Suppose hippo is declared as the formal parameter to a function and iguana is used as an actual parameter. That, is, we might have

void 
zebra (char *hippo)
{
  printf ("%s\n", hippo);
}
// ...
  zebra (iguana);

or

void
yak (char hippo[])
{
  printf ("%s\n", hippo);
}
// ...
  yak (iguana);

Determine experimentally whether it matters whether iguana is declared as char *iguana or char iguana[size].

Exercise 5: Concatenating Strings

Consider the folling declarations:

  char jackal[8] = "animal";
  char koala[8] = "animal";
  char lemur[8] = "animal";

a. What do you expect the effect of the following instruction to be?

  strcat (koala, "s");

b. Check your answer experimentally. Make sure to print out all three strings.

c. Explain the output.

d. Assume you didn't execute the previous strcat instruction. What do you expect the effect of the following instruction to be?

  strcat (koala, "istic");

e. Check your answer experimentally. Make sure to print out all three strings.

f. Explain the output.

g. Assume you didn't execute the previous strcat instructions. What do you expect the effect of the following instruction to be?

  strcat (koala, "ed");

h. Check your answer experimentally. Make sure to print out all three strings.

i. Explain the output.

Exercise 6: Changing Strings

Consider the following function.

void
llama (char *monkey)
{
  monkey[0] = 'M';
} // llama

a. What do you expect the following code to do?

  char narwahl[] = "donkey";
  printf ("Original string: %s\n", narwahl);
  llama (narwahl);
  printf ("Updated string: %s\n", narwahl);

b. Check your answer experimentally.

c. What do you expect the following code to do?

  char *narwahl = "donkey';
  printf ("Original string: %s\n", narwahl);
  llama (narwahl);
  printf ("Updated string: %s\n", narwayl);

d. Check your answer experimentally.

e. What do you expect the following code to do?

  llama ("donkey");

f. Check your answer experimentally.

Exercise 7: Reversing Strings

Write a procedure, char *reverse (char *str), that reverses the order of characters in str and returns the modified str. Your procedure should modify str.

Exercise 8: Reversing Strings, Revisited

a. Change the parameter to reverse to read const char *str.

b. What effect do you expect this change to have?

c. Check your answer experimentally.

d. Remove the const declaration.

Exercise 9: Reversing Strings, Re-Revisited

Why do you think I did not have you write reverse so that it returns a new string?

Discuss your answer with your neighbor. Be prepared to discuss it with the class.

For Those with Extra Time

Extra 1: Selecting Substrings

Write a procedure that takes two values, a string and a character, and prints out all the substrings of the string that start with that character. For example,

  print_substrings ("twas brillig and the slithy toves did gyre", 't');

would result in output of

twas brilling and the slithy toves did gyre
the slighty toves did gyre
ty toves did gyre
toves did gyre

Similalry,

  print_substrings ("twas brillig and the slithy toves did gyre", ' ');

would result in output of

 brilling and the slithy toves did gyre
 and the slithy toves did gyre
 the slithy toves did gyre
 slithy toves did gyre
 toves did gyre
 did gyre
 gyre

Finally,

  print_substrings ("twas brillig and the slithy toves did gyre", 'z');

would result in no output.

Hint: You will probably find benefit to using strchr.

Extra 2: The Name Game

Write a program that implements the Name Game.

 

History

Thursday, 13 February 2003 [Samuel A. Rebelsky]

  • Planned design.

Friday, 14 February 2003 [Samuel A. Rebelsky]

Monday, 1 November 2010 [Samuel A. Rebelsky]

Tuesday, 5 April 2011 [Samuel A. Rebelsky]

  • Lots of minor cleanup.

Friday, 8 April 2011 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri Apr 8 11:40:37 2011.
The source to the document was last modified on Fri Apr 8 11:40:35 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/strings-lab.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu