The purpose of this quiz is to give you a chance to focus your knowledge of C-strings in C++.
All C-strings end in what character? Is this character in the last array position?
The '\0' character ends all C-strings. It is also known as the null
character.
It is not necessarily in the last array position. It immediately follows
the last char of the user's data.
The C-string library contains the functions _____, _____, and _____ to replace the operations =, +=, and (<,>,<=,>=,==,!=) respectively.
| TRUE ✗ | FALSE ✓ | We typically process C-strings one element at a time like all other arrays (for input, output, assignment, etc.). | ||
|---|---|---|---|---|
| TRUE ✗ | FALSE ✓ | There is no need to protect C-strings from overflow on input as we did with other arrays. | ||
| TRUE ✓ | FALSE ✗ | When passing a C-string to a function, we normally don't pass the length of the array/data. | ||
| TRUE ✗ | FALSE ✓ | Functions which process C-string arguments typically call strlen and then use a for loop to process the elements. |
An array of C-strings is a _____ dimensional structure. Access using _____ indices arrives at a single character within a single string. Access using _____ index(es) arrives at a single string. Using no indices refers to _____.
To protect assignment from overflow, the cstring library provides another version of the strcpy function. This new version has a(n) letter n in its name and a third argument which specifies a maximum number of characters to copy. This version will copy characters from the source to destination C-string until either the null character is copied or it copies the maximum number of characters.
There is, in general, no guarantee that a null character is stored in the result array. To alleviate this situation, it is normal for a programmer to store one him/herself after a call to this function. (If they didn't, after all, their array would cease to be a C-string!!!)
Draw two pictures of an array of C-strings. One should be logical, the other more realistic. (Show the array containing contents such as: "Estelle", "Yrda", "Sue", "John", "Vong", "Dmitry", "Ng", "George".)
Logical: Realistic:
+=========+ +====+====+====+====+====+====+====+====+
0 | Estelle | 0 | E | s | t | e | l | l | e | \0 |
+=========+ +====+====+====+====+====+====+====+====+
1 | Yrda | 1 | Y | r | d | a | \0 | | | |
+=========+ +====+====+====+====+====+====+====+====+
2 | Sue | 2 | S | u | e | \0 | | | | |
+=========+ +====+====+====+====+====+====+====+====+
3 | John | 3 | J | o | h | n | \0 | | | |
+=========+ +====+====+====+====+====+====+====+====+
4 | Vong | 4 | V | o | n | g | \0 | | | |
+=========+ +====+====+====+====+====+====+====+====+
5 | Dmitry | 5 | D | m | i | t | r | y | \0 | |
+=========+ +====+====+====+====+====+====+====+====+
6 | Ng | 6 | N | g | \0 | | | | | |
+=========+ +====+====+====+====+====+====+====+====+
7 | George | 7 | G | e | o | r | g | e | \0 | |
+=========+ +====+====+====+====+====+====+====+====+
Write a function which can change a C-string to all uppercase.
void toupper(char s[])
{
size_t c{0};
while ( s[c] != '\0' )
{
s[c] = static_cast<char>(toupper(s[c]));
}
return;
}
Code the declaration of the array you drew above. Use your function from above to uppercase the contents of this array. (This code needn't be a whole program. A fragment will do.)
const size_t MAX_NAME_LENGTH{8};
const size_t MAX_NAMES{8};
char names[MAX_NAMES][MAX_NAME_LENGTH] = { "Estelle", "Yrda", "Sue",
"John", "Vong", "Dmitry",
"Ng", "George" };
size_t num_names = 8;
for (size_t n = 0; n < num_names; ++n)
{
toupper(names[n]);
}
The following function is terribly program specific:
double input(void)
{
double pay;
cout << "Enter your pay rate: ";
cin >> pay;
return pay;
}
To fix it, we'd normally do something like this:
// caller must prompt before calling us!!!
double input(void)
{
double x;
cin >> x;
return x;
}
But with C-strings, we can now leave the prompt in the function and still have the function completely generic. Show how this can be done:
double input(const char prompt[])
{
double x;
cout << prompt;
cin >> x;
return x;
}
| TRUE ✓ | FALSE ✗ | Having a function take C-string arguments can extend its usefulness to many new situations. | ||
|---|---|---|---|---|
| TRUE ✓ | FALSE ✗ | Such C-string arguments are often 'pass-by-value' (which means they must be modified with the keyword const). | ||
| TRUE ✓ | FALSE ✗ | The actual arguments passed to these functions can be either real arrays or literal C-strings. |