What We Will Cover
Continuations
Homework Questions?
^ top
6.1: More About Strings and Characters
Learner Outcomes
At the end of the lesson the student will be able to:
- Iterate through a string and extract each character
- Convert characters to digits
- Use string functions
|
^ top
6.1.1: Strings Versus Characters
- Remember that a string is a series of characters enclosed in double quotes such as:
"Hello" "b" "3.14159" "$3.95" "My name is Ed"
- We can store text in a variable of type
string , like:
string firstName; // definition
firstName = "Edward"; // assignment
string lastName = "Parrish"; // definition + assignment
cout << firstName << " " << lastName << endl;
- On the other hand, a character is a single letter, number or special symbol
- We enclose characters in a single quote, rather than a double quote, like:
'a' 'b' 'Z' '3' 'q' '$' '*'
- To store a single character, we use a variable of type
char , such as:
char letterA = 'A';
char letterB = 'B';
- Each character is stored as a number, using its ASCII Table value
- By declaring a
char variable, or by using single quotes, C++ knows to treat the number as a character
- Thus, when we print a character, we see a letter rather than a number:
char letter = 'A';
cout << letter << 'B' << endl;
- As we can see, a string is made up of characters and characters are numerical codes
- We can use this information to work with characters and strings
String Concatenation and Functions
- Recall that we can join (concatenate) two strings or a string with a character
string str = "abc";
str = str + "1"; // allowed
str = str + '1'; // allowed
str = str + 1; // NO
str = str + 1.2; // NO
- However, we cannot concatenate a string with a number
- Because strings are objects, they have member functions
- Two useful member functions we have studied are
length() and substr()
- length(): Returns the number of characters in a string
string str = "Hello";
cout << "The number of characters is " << str.length()
<< ".\n";
- substr(i, n): Returns a substring of length n starting at index i
string greeting = "Hello, World!\n";
string sub = greeting.substr(0, 4);
cout << sub << endl;
- The position numbers in a string start at 0. The last character is always one less than the length of the string
H |
e |
l |
l |
o |
, |
|
W |
o |
r |
l |
d |
! |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
string w = greeting.substr(7, 5);
H |
e |
l |
l |
o |
, |
|
W |
o |
r |
l |
d |
! |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
Check Yourself
- True or false: strings are a sequence of characters.
- True or false: "A" and 'A' are the same.
- The following code is wrong because ________.
cout << "3.14159" * 2;
- you cannot double PI
- 3.14159 is not exact enough to represent PI
- strings may be added but not multiplied
- "3.14159" is not a number
- After the following code executes, it displays ________.
char ch;
ch = 'd' - 'a' + 'A';
cout << ch << endl;
- 'D'
- D
- 68
- d
- When placed after the starter code, which of the following will compile in C++-11?
string str = "abc"; // starter code
str = str + "1";
str = str + '1';
str = str + 1;
str = str + 1.2;
^ top
6.1.2: Indexing a String
Converting a Character to a Digit
Check Yourself
- For the following string definition, answer the questions below:
string str = "C++ Rules!";
- The value of
str[0] is: ________
- The value of
str[2] is: ________
- The value of
str[4] is: ________
- The value of
str[str.length() - 1] is: ________
- True or false: To extract a character from a string use square brackets
[] .
- True or false: To extract a string from a string use the
substr() function.
^ top
6.1.3: Iterating Strings
Using unsigned
Exceeding String Length
Check Yourself
- The keyword
unsigned makes a variable only represent ________ numbers.
- The keyword
unsigned defaults to ________.
int
float
double
string
- True or false: C++ always throws an error when an index inside
[ ] exceed its bounds.
- The problem with the following code is ________.
string str = "abc";
for (unsigned i = 0; i <= str.length(); i++)
{
cout << str[i] << endl;
}
- nothing
- the word
unsigned is not a valid type
- printing a value outside the bounds of the string
- one cannot use square brackets [ ] with a string
^ top
Exercise 6.1a: Iterating Strings (4m)
In this exercise we iterate a string.
- Copy the following program into a text editor, save it as
letters.cpp , and then compile and run the starter program to make sure you copied it correctly.
#include <iostream>
using namespace std;
int main() {
// Enter your code here
return 0;
}
- Add the code to prompt for and read a messages from the user:
cout << "Enter a word: ";
string msg;
cin >> msg;
- Next add the following
for -loop code to the main() function.
for (unsigned int i = 0; i < msg.length(); i++)
{
cout << i << ": " << msg[i] << endl;
}
- Compile and run your code. What do you see when you compile and run?
- Be prepared to answer the following Check Yourself questions when called upon.
When finished, verify your source code against the following and then please help those around you. Source code need not be exactly the same.
Completed Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <iostream>
using namespace std;
int main() {
cout << "Enter a word: ";
string msg;
cin >> msg;
for (unsigned int i = 0; i < msg.length(); i++)
{
cout << i << ": " << msg[i] << endl;
}
return 0;
}
|
Check Yourself
- True or false: the
length() function of a string returns an unsigned integer.
- For the following code, the output the second time through the loop is ________
string msg = "aeiou";
for (unsigned i = 0; i < msg.length(); i++) {
cout << "Char[" << i << "]: " << msg[i] << endl;
}
- True or false: the compiler may give a warning if you compare an
unsigned int with a signed int .
- Each character in the above loop is printed on it own line because of the ________.
^ top
6.1.4: String Input With Spaces
Input Using getline()
- To read an entire line we use function
getline()
- Syntax:
getline(cin, stringVariable);
- Where:
- stringVariable: the name of the string variable
- For example:
string line;
cout << "Enter a line of input:\n";
getline(cin, line);
cout << line << "END OF OUTPUT\n";
- Note that
getline() stops reading when it encounters a '\n'
The Problem with Newlines
- When you press the Enter key, a newline character (
'\n' ) is inserted as part of the input
- The newline character can cause problems when you mix
cin >> with getline()
- Recall that
cin >> s1 :
- Skips whitespace
- Reads non-whitespace characters into the variable
- Stops reading when whitespace is found
- Since whitespace includes newline characters, using
cin >> will leave a newline character in the input stream
- However,
getline() just stops reading when it first finds a newline character
- This can lead to mysterious results in code like the following:
cout << "Enter your age: ";
int age;
cin >> age;
cout << "Enter your full name: ";
string name;
getline(cin, name);
cout << "Your age: " << age << endl
<< "Your full name: " << name << endl;
- To correct this problem we use
cin >> ws just before getline()
cin >> ws; // clear whitespace from input stream
- We can see how to use this fix in the following example
Example Using cin >> ws
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <iostream>
using namespace std;
int main() {
cout << "Enter your age: ";
int age;
cin >> age;
cout << "Enter your full name: ";
string name;
cin >> ws; // clear whitespace from buffer
getline(cin, name);
cout << "Your age: " << age << endl
<< "Your full name: " << name << endl;
return 0;
}
|
Check Yourself
- True or false: Using the >> operator with string variables only reads one word at a time.
- To read strings containing multiple words use the ________ function.
- True or false: before you switch from using the >> operator to using
getline() , you must clear the next newline character from the input buffer.
- To clear whitespace from the input buffer use: ________.
- The following code has a problem.
1 int age;
2 string name;
3 cout << "Enter your age: ";
4 cin >> age;
5 cout << "Enter your full name: ";
6 getline(cin, name);
To fix the problem, we insert the following statement between lines ________.
cin >> ws;
- 1 and 2
- 2 and 3
- 3 and 4
- 4 and 5
^ top
6.1.5: Processing Text Input
- Sometimes we need to read input as words and sometimes as lines
- To input a sequence of words, use the loop:
string word;
while (cin >> word) {
// process word
cout << word << endl;
}
cin >> word is the same test as cin.good() (see lesson 5.3.7)
- To process input one line at a time, use the
getline() function
string line;
while (getline(cin, line)) {
// process line
cout << line << endl;
}
getline(cin, line) returns true as long as there is input remaining
- The following example processes text input by counting words
- When reading input in the
while test, you need to close the stream using:
- Windows: Ctrl+Z
- Linux,OS-X: Ctrl+D
- Closing the stream acts as a sentinel value for the loop
- When the stream fails the loop exits
Example Program that Counts Words in a File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Enter a phrase followed by the Enter"
<< " key and Ctrl-Z/D.\n";
string word;
int count = 0;
while (cin >> word) {
count++;
}
cout << "Number of words: " << count << endl;
return 0;
}
|
Redirection of Input and Output
Check Yourself
- True or false: the following code reads input one word at a time.
string str;
while (cin >> str) {
cout << str << endl;
}
- True or false: the following code reads input one line at a time.
string str;
while (getline(cin, str)) {
cout << str << endl;
}
- To close the
cin input stream use the Ctrl key plus the ________ key.
- True or false: most operating systems let you redirect input and output at the command line.
^ top
Exercise 6.1b: Finding Words (5m)
In this exercise we write code to find words in a text file. Compile and test after each step to verify your work.
Specifications
- Copy the following program into a text editor, save it to the home folder of Cygwin or your Terminal window as
findword.cpp , and then compile and run the starter program to make sure you copied it correctly.
#include <iostream>
using namespace std;
int main() {
// Enter your code here
return 0;
}
- Inside
main() , declare both a string variable named word and an integer variable named count , like:
string word;
int count = 0;
- Add a while loop to read one word at a time from
cin , like:
while (cin >> word) {
// Add if statements here
}
- Inside the while loop write code to add one to the
count variable.
count++;
- Add two if-statements, one to test for the word "Shazam" and one to test for the word "bogus", reporting the word count where the word was found. For example:
if (word == "Shazam") {
cout << "Shazam is word " << count << endl;
}
- Test your program by saving the words.txt file into the home folder of Cygwin or your Terminal window.
words.txt
- Run the program from the command line using input redirection:
./findword < words.txt
- Save your program source code to submit to Canvas as part of assignment 6.
When finished, verify your source code against the following and then please help those around you. Source code need not be exactly the same if the program works.
Completed Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <iostream>
using namespace std;
int main() {
cout << "Enter a phrase followed by the Enter"
<< " key and Ctrl-Z/D.\n";
string word;
int count = 0;
while (cin >> word) {
count++;
if (word == "shazam") {
cout << "Shazam is word " << count << endl;
} else if (word == "bogus") {
cout << "bogus is word " << count << endl;
}
}
return 0;
}
|
As time permits, read the following sections and be prepared to answer the Check Yourself questions in the section: 6.1.6: Summary.
^ top
6.1.6: Summary
- A string is a series of characters enclosed in double quotes
- We can store text in a variable of type
string , like:
string s1 = "Hello Mom!";
- A character is a single letter, number or special symbol
- We can store a a single character using a variable of type
char , such as:
char letterA = 'A';
char letterB = 'B';
- Each character is stored as a number, using its ASCII code
- Strings are stored in a character sequence starting at 0 (zero)

- We can access individual characters of a
string using []
- Strings are a special type of variable called objects
- Because a string is an object, it has member functions
- We can iterate through a string using a loop and the
length() member function:
string s = "abcdef";
for (unsigned i = 0; i < s.length(); i++) {
cout << "Char[" << i << "]: " << s[i] << endl;
}
- To read an entire line, you need to use the
getline() function:
getline(cin, line);
- Sometimes
cin >> can leave a '\n' character in the input stream
- To get around this problem you can use
cin >> ws before getline()
cin >> ws; // clear whitespace from buffer
Check Yourself
Answer these questions to check your understanding. You can find more information by following the links after the question.
- String are enclosed in double quotes. What type of quote marks enclose characters? (6.1.1)
- The characters of a string variable can be accessed using what brackets? (6.1.2)
- The leftmost character of a string is accessed using which index number? (6.1.2)
- To print the following string vertically down the page, what code do you write? (6.1.3)
string str = "Hi mom!";
- To convert the following
char variable to a number, what code do you write? (6.1.4)
char ch = '7';
- What is the value of the expression: 'd' - 'a' + 'A'? (6.1.4)
- To convert the following string variable to a number, what code do you write? (6.1.4)
string str = "7";
- How many words can you enter with the following code? (6.1.5)
string something;
cout << "Enter something: ";
cin >> something;
cout << "You entered: " << something << endl;
- How can you change the previous code to read a string that includes spaces? (6.1.5)
- What code can you use to clear newlines and other whitespace from the input stream? (6.1.5)
^ top
6.2: Preparing for Midterm 1
Learner Outcomes
At the end of the lesson the student will be able to:
- Review possible exam questions
- Know the study aids available for the exam
|
^ top
6.2.1: Reviewing the Exam Topics
- Remember that we each chose a test topic last class meeting
- Your homework, due two days before the exam, was to develop at least 5 possible test questions for your selected topic
- We will break into groups and review the questions
Test Review Exercise (10m)
- Within your group, review each set of test questions for accuracy and testing efficacy
- Discuss ways to improve test questions and update postings in Canvas
- Choose one excellent question to review with the entire class.
- Write the question on the board.
^ top
6.2.2: Practicing for an Exam
- One of the purposes of an exam is to give us a chance to review the material we have covered
- Practicing for an exam is important to doing our best
- We do not want to wait until the night before to prepare
- To help us practice we will review some problems during class
- Treat the practice like homework -- because it is!
- Anything not completed in class must be completed at home and turned in before the test
Midterm 1 Review Supplements
^ top
6.2.3: Summary of Test Preparation Materials
- To help your understanding of how the midterm will operate, I have provided a practice exam in Canvas
- The questions are intended to help you get a "feel" for taking an exam in Canvas
- The questions are NOT intended to tell you everything that is on the exam
- Suggestion: prepare first and then try the practice exam
- This will give you a better understanding of how much more preparation you need
Summary of Test Preparation Resources
For a complete list see lesson 5.4.3: Test Preparation Resources.
- Practice midterm
- Review worksheet turned in as part of A5-Midterm 1 Preparation
- Reviewing student-prepared study questions (in Canvas)
- Reviewing lecture notes
- Reviewing programming projects
- Reviewing CodeLab questions
^ top
Wrap Up
Due Next: Quiz 6 (Canvas) (3/10/20)
A6-Loopy Programs (3/12/20)
- When class is over, please shut down your computer
- You may complete unfinished exercises at the end of the class or at any time before the next class.
^ top
Last Updated: April 04 2020 @01:43:41
|