
Chapter 11 Self Test Exercises
1. Which of the following declarations are equivalent?
char string_var[10] = “Hello”;
char string_var[10] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
char string_var[10] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’};
char string_var[6] = “Hello”;
char string_var[] = “Hello”;
2. What C string will be stored in singing_string after the following code is run?
char singing_string[20] = “DoBeDo”;
cout strcat(singing_string, “ to you”);
Assume that the code is embedded in a complete and correct program and that an include
directive for <cstring> is in the program file.
3. What (if anything) is wrong with the following code?
char string_var[] = “Hello”;
strcat(string_var, “ and Good-bye.”);
cout << string_var;
Assume that the code is embedded in a complete and correct program and that an include
directive for <cstring> is in the program file.
4. Suppose the function strlen (which returns the length of its string argument) was not
already defined for you. Give a function definition for strlen. Note that strlen has
only one argument, which is a C string. Do not add additional arguments; they are not
needed.
5. What is the length (maximum) of a string that can be placed in the string variable declared
by the following declaration? Explain.
char s[6];
6. How many characters are in each of the following character and string constants?
a.‘\n’
b.‘n’
c.“Mary”
d.“M”
e.“Mary\n”
7. Since character strings are just arrays of char, why does the test caution you not to confuse
the following declaration and initialization?
char short_string[] = “abc”;
char short_string[] = {‘a’, ‘b’, ‘c’};