CSC161 2010F Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
-
[Assignment]
[Lab]
Groupings:
[EBoards]
[Assignments]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Summary: In this laboratory, you will experiment with C's strings and the various standard string procedures.
Contents:
Create a new directory for this laboratory. I'd suggest calling it
Labs/Strings
.
Consider the following two mechanisms for printing the string
antelope
.
a. Which do you prefer? Why?
printf(antelope);
printf("%s", antelope);
b. Write a simple program in which the two statements have different output.
Here are a number of different string declarations.
char *baboon; char *chimpanzee = "animal"; char dolphin[]; char emu[] = "animal"; char fox[8]; char fox[8] = "animal";
a. Which are valid and which are invalid?
b. How do the valid declarations differ?
a. Consider the following declaration and code
char gorilla[8]; char ch; ... printf("sizeof(gorilla): %d\n", sizeof(gorilla)); printf("strlen(gorilla): %d\n", strlen(gorilla)); printf("gorilla: '%s'\n", gorilla);
What do you expecct the output to be?
b. How would your answer change if gorilla
were declared with
char *gorilla;
c. Verify your hypotheses experimentally.
d. What do your results suggest?
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.)
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,"istic");
You may find it useful to read the man page for strcat
.
b. Verify your answer experimentally. Make sure to print out all three strings.
c. Explain your answer.
d. Assume you didn't execute the instruction from step a. What do you expect the effect of the following instruction to be?
strcat (koala, "ed");
e. Verify your answer experimentally. Make sure to print out all three strings.
f. Explain your answer.
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
.
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.
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.
Write a program that implements the Name Game.
Thursday, 13 February 2003 [Samuel A. Rebelsky]
Friday, 14 February 2003 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS195/2003S/Labs/strings.html
.
Monday, 1 November 2010 [Samuel A. Rebelsky]
reverse
problem.
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/strings-lab.html
.
[Skip to Body]
Primary:
[Front Door]
[Schedule]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
-
[Assignment]
[Lab]
Groupings:
[EBoards]
[Assignments]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
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 Tue Apr 5 23:06:46 2011.
The source to the document was last modified on Tue Apr 5 23:06:44 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/strings-lab.html
.