Deck 9: Strings

Full screen (f)
exit full mode
Question
If you want to examine an input value without actually removing it from the input stream,you can apply a member function of cin to the variable ch,which you are to assume is a char type variable.Which of these does this task?

A)cin.get(ch);
B)cin.put(ch);
C)cin.peek(ch);
D)cin.putback(ch);
Use Space or
up arrow
down arrow
to flip the card.
Question
It is legal to assign C-string variables.
Question
The C-string library functions are safe and require no special care.
Question
The preprocessor symbol NULL and the C-string terminator,the null character share the value 0
Question
The C-string library functions use the null terminator to decide when to stop processing.
Question
It is possible to use a standard string with a string member function as source for the strcpy or strcat functions.
Question
To read a character at a time,or to write a character at a time,declare a character variable ch and write this:
cin >> ch;
Question
The C-string library function strcmp compares two strings for equal length.
Question
The = sign used to give a variable an initial value in a definition,
char ch = 'A';
is different from the = sign in an assignment.
ch = 'B';
Question
Indexing for Standard string objects does not range check/
Question
If you have already removed a character (in char variable ch)from the input stream,you can apply a member function of cin to the variable ch to return it to the input stream.Which of these does this task?

A)cin.get(ch);
B)cin.put(ch);
C)cin.peek(ch);
D)cin.putback(ch);
Question
A C-string variable is just an array of characters.
Question
A C-string is a sequence of characters terminated by the null character.
Question
ANSI/ISO C++ standard includes a string-handling library in the form of the class string.
Question
C-string literals are written 'Hello'.
Question
A C-string variable is an array,so it can be indexed to access individual character positions (indexed variables of type character).
Question
A C-string variable with a something in it is always filled with characters.
Question
You can assign a standard string to a C-string variable.
Question
To get a complete line of 70 or so character into a C-string is as simple as declaring a long enough C-string and then using the >> operator to extract the line from the input.
Question
It is illegal to write char message[] ="Go Home!";
Question
What is the problem (if any)with this code?
char s1[20] = "How you been? ";
strcat(s1,"Fine as wine");
Question
The header file that you must #include to have access to the Standard Library string class is

A)C-string
B)string.h
C)string
D)String.h
E)None of the above
Question
Write code to define a C-string aString variable,of size 10 but do not initialize it.Use a library C-string library function to copy the C-string literal,"Hello" into aString.
Question
Given the following definition and initialization.Write a code fragment including a loop that will overwrite the string greeting,with 'X' in all character position,keeping the length the same.
Question
Give the output the following code fragment would produce if embedded in a correct and complete program?
char singing[20] = "Hi De Hi";
strcat(singing,",Hi De Ho!");
Question
What is a simple way to convert a string to a numeric value in C++11? This is sometimes needed when data is input as a string but we need to perform numeric computations on the values.
Question
Write a C++ program fragment that will read a line of text and echo the line with all uppercase letters deleted.Declare variables and include necessary header.
Question
Write a complete program with necessary #include directives to copy the C-string constant "Hello" into the string variable aString,declared below.
char aString[10];
Question
On most systems,you don't get a chance to enter the letter when the code given below is run.Write two different code fragments that repair the problem.
cout << "Enter a number:\n";
int number;
cin >> number;
cout << "Enter a letter;\n";
char symbol;
cin.get(symbol);
cout << number << " " << symbol << endl;
Dialog: (Computer output is bolded. )
Enter a number:
21
Now enter a letter:
21
On my system,like many systems,one doesn't get a chance to type the letter.
Question
What is the longest C-string that can be put into this C-string variable? Explain.
char s[9];
Question
Write a line of code that declares and initializes a C-string variable,yourString,that can hold up to 10 characters.Use the initialization string "Whoops".
Question
Does this code have a problem?
char strVar[] = "Hello";
strcat(strVar," And Good-bye.");
cout << strVar << endl;
Question
Write a short C++ code fragment that reads a line of text and echo the line with all uppercase letters replaced by asterisks.
Question
Reimplement the function strlen.Call your function strLen.The library function,strlen takes one char str[] argument,so you give strLen a char str[] argument as well.
Question
Write an if-else statement using a C-string function that reports whether two C-strings variables contain the same C-string.
Question
You are writing bulletproof input.Describe a simple situation in this setting where you need to know what character has been entered by the user of your program before you remove it from the input stream.Just describe the setting,do not attempt to write code for this.
Question
What is the problem with this bit of code? On most systems,you don't get a chance to enter the letter.Why?
cout << "Enter a number:\n";
int number;
cin > number;
cout << "Enter a letter;\n";
char symbol;
cin.get(symbol);
cout << number << " " << symbol << endl;
Dialog: (Computer output is bolded. )
Enter a number:
21
Now enter a letter:
21
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/37
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 9: Strings
1
If you want to examine an input value without actually removing it from the input stream,you can apply a member function of cin to the variable ch,which you are to assume is a char type variable.Which of these does this task?

A)cin.get(ch);
B)cin.put(ch);
C)cin.peek(ch);
D)cin.putback(ch);
C
2
It is legal to assign C-string variables.
False
3
The C-string library functions are safe and require no special care.
False
4
The preprocessor symbol NULL and the C-string terminator,the null character share the value 0
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
5
The C-string library functions use the null terminator to decide when to stop processing.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
6
It is possible to use a standard string with a string member function as source for the strcpy or strcat functions.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
7
To read a character at a time,or to write a character at a time,declare a character variable ch and write this:
cin >> ch;
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
8
The C-string library function strcmp compares two strings for equal length.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
9
The = sign used to give a variable an initial value in a definition,
char ch = 'A';
is different from the = sign in an assignment.
ch = 'B';
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
10
Indexing for Standard string objects does not range check/
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
11
If you have already removed a character (in char variable ch)from the input stream,you can apply a member function of cin to the variable ch to return it to the input stream.Which of these does this task?

A)cin.get(ch);
B)cin.put(ch);
C)cin.peek(ch);
D)cin.putback(ch);
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
12
A C-string variable is just an array of characters.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
13
A C-string is a sequence of characters terminated by the null character.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
14
ANSI/ISO C++ standard includes a string-handling library in the form of the class string.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
15
C-string literals are written 'Hello'.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
16
A C-string variable is an array,so it can be indexed to access individual character positions (indexed variables of type character).
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
17
A C-string variable with a something in it is always filled with characters.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
18
You can assign a standard string to a C-string variable.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
19
To get a complete line of 70 or so character into a C-string is as simple as declaring a long enough C-string and then using the >> operator to extract the line from the input.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
20
It is illegal to write char message[] ="Go Home!";
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
21
What is the problem (if any)with this code?
char s1[20] = "How you been? ";
strcat(s1,"Fine as wine");
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
22
The header file that you must #include to have access to the Standard Library string class is

A)C-string
B)string.h
C)string
D)String.h
E)None of the above
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
23
Write code to define a C-string aString variable,of size 10 but do not initialize it.Use a library C-string library function to copy the C-string literal,"Hello" into aString.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
24
Given the following definition and initialization.Write a code fragment including a loop that will overwrite the string greeting,with 'X' in all character position,keeping the length the same.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
25
Give the output the following code fragment would produce if embedded in a correct and complete program?
char singing[20] = "Hi De Hi";
strcat(singing,",Hi De Ho!");
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
26
What is a simple way to convert a string to a numeric value in C++11? This is sometimes needed when data is input as a string but we need to perform numeric computations on the values.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
27
Write a C++ program fragment that will read a line of text and echo the line with all uppercase letters deleted.Declare variables and include necessary header.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
28
Write a complete program with necessary #include directives to copy the C-string constant "Hello" into the string variable aString,declared below.
char aString[10];
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
29
On most systems,you don't get a chance to enter the letter when the code given below is run.Write two different code fragments that repair the problem.
cout << "Enter a number:\n";
int number;
cin >> number;
cout << "Enter a letter;\n";
char symbol;
cin.get(symbol);
cout << number << " " << symbol << endl;
Dialog: (Computer output is bolded. )
Enter a number:
21
Now enter a letter:
21
On my system,like many systems,one doesn't get a chance to type the letter.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
30
What is the longest C-string that can be put into this C-string variable? Explain.
char s[9];
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
31
Write a line of code that declares and initializes a C-string variable,yourString,that can hold up to 10 characters.Use the initialization string "Whoops".
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
32
Does this code have a problem?
char strVar[] = "Hello";
strcat(strVar," And Good-bye.");
cout << strVar << endl;
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
33
Write a short C++ code fragment that reads a line of text and echo the line with all uppercase letters replaced by asterisks.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
34
Reimplement the function strlen.Call your function strLen.The library function,strlen takes one char str[] argument,so you give strLen a char str[] argument as well.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
35
Write an if-else statement using a C-string function that reports whether two C-strings variables contain the same C-string.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
36
You are writing bulletproof input.Describe a simple situation in this setting where you need to know what character has been entered by the user of your program before you remove it from the input stream.Just describe the setting,do not attempt to write code for this.
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
37
What is the problem with this bit of code? On most systems,you don't get a chance to enter the letter.Why?
cout << "Enter a number:\n";
int number;
cin > number;
cout << "Enter a letter;\n";
char symbol;
cin.get(symbol);
cout << number << " " << symbol << endl;
Dialog: (Computer output is bolded. )
Enter a number:
21
Now enter a letter:
21
Unlock Deck
Unlock for access to all 37 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 37 flashcards in this deck.