COSC 1337 - Programming Fundamentals II


Chapter 12 More About Characters, Strings and the string Class

Review Exercises

I recommend that you review the chapter by doing these exercises. I will NOT grade these exercises.

Consider reviewing the Checkpoint questions from this chapter.
Answers to Checkpoint questions are in Appendix P (on the CD that came with your textbook).

Review Questions and Exercises: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 23, 27, 29.
Answers to Review Questions and Exercises are in Appendix Q (on the CD that came with your textbook).

Other Questions

1. Write the declaration for a C-string (character array) called paperColor (it should be able to hold a 40 character color name plus the null terminator character). Then write code to input a value for paperColor from the keyboard. (You can assume that the input will not contain any blanks.)

2. Using your variable from question 1, write code to set variable paperPrice to 3.95 if paperColor is "blue".

3. Using your variable from question 1, write code to store "beige" in paperColor. Then write code to print the value of the variable (include a descriptive label).

4. Write the declaration for a C-string (character array) called sentence (it should be able to hold 80 characters plus the null terminator character). Then write code to input a value for sentence from the keyboard. (Assume that the input is on a line by itself and that it may contain blanks.)

5. To copy one string into another, you should use the strcpy() function (the assignment operator will not work). To help you understand how C strings work, try to write your own version of the strcpy() function. Make it a void function. Your function prototype should look like this:

void strcpy( char toString[], char fromString[] );

6. Given the code segment:

char string1[31], string2[31]; 
cout << "Type a sentence and press enter: " << endl;
cin >> string1;
cin.getline(string2, 31);
cout << string1 << endl;
cout << string2 << endl;

What is printed if the input is "It sure is hot today."?

7. Write the declaration for a string object called paperColor. Then write code to input a value for paperColor from the keyboard. (You can assume that the input will not contain any blanks.)

8. Using your variable from question 7, write code to set variable paperPrice to 3.95 if paperColor is "blue".

9. Using your variable from question 7, write code to store "beige" in paperColor. Then write code to print the value of the variable (include a descriptive label).

10. Write the declaration for a string object called sentence. Then write code to input a value for sentence from the keyboard. (Assume that the input is on a line by itself and that it may contain blanks.)

11. Given the code segment:

string string1, string2; 
cout << "Type a sentence and press enter: " << endl;
cin >> string1;
getline(cin, string2);
cout << string1 << endl;
cout << string2 << endl;

What is printed if the input is "It sure is hot today."?

Answers to Other Questions are here.


Creative Commons License
© Austin Community College 2016. The content on this page is licensed under a
Creative Commons Attribution 4.0 International License.

Copyright: © 2013 by the Austin Community College
Department of Computer Studies. All rights reserved.
Last updated: July 15, 2013