We will not be studying the material on pp.
351-374 of Ch. 9 in this course.
Objective #1: Understand C++ strings.
- A string is a sequence of characters that may or may not spell
a word.
- A string literal is a sequence of characters that is used in
a C++ program between double quotes such as in the statement
cout << "Hello world!";
where "Hello world!" is the string literal. Note that the
string literal, in this case, includes a space and an exclamation point.
- A character literal is a single character that is used in a
C++ program between single quotes. In the statement,
initial
= 'T';
the character literal is 'T'.
Objective #2: Use string variables.
- When one needs to refer to a variable that contains multiple characters,
he or she needs to use a string variable.
- You must declare a string variable in your program before you wish
to use the variable.
- Example: string
studentName;
- Example: string
studentName = "John Doe";
declares AND initializes the variable, studentName, in the same
statement.
- You can use the assignment operator to assign the value of one string
to another string as in:
studentName = personName;
- In order to use string variables within any C++program, you must use
the compiler directive,
#include <string>
at the top of your program. You must also refer to iostream.h header
file as iostream and you must add the statement,
using namespace std;
below your last #include directive.
The following program uses a string variable and properly includes the
necessary header files,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cin >> name;
cout << name;
return 0;
} // end of main
In this example, the user would only be able to type in one word such
as his or her first name. If the user attempted to type his first and
last names separated by a space, only the first name would be stored
in the string variable name. In order to allow the user to enter a whole
name (or any combination of two or more words separated by blank spaces),
you must use the getline function that
is explained below.
- The getline function
can be used to allow the user to input a string that includes one or
more blank spaces. By using the getline function, everything that the
user types until he/she presses the Enter key is stored in a specified
string variable.
- The following example would allow a whole name such as "John
Doe" to be stored in the string variable name.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
getline(cin, name);
cout << name;
return 0;
} // end of main
- If a cin input statement precedes your getline
statement, you may need to use the ignore
function to "flush the buffer" and get rid of any extra new
line characters that the user inputed when he or she inputed a previous
value.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int age;
string name;
cout << "Enter your age: ";
cin >> age;
cin.ignore(100, '\n');
cout << "Enter your full name:
";
getline(cin, name);
cout << name;
return 0;
} // end of main
The cin.ignore statement tells the
compiler to ignore the next 100 characters or everything entered up
to the next press of the Enter key. I use 100 characters simply because
that is usually enough characters to ignore and more importantly it
is only the extra press of the Enter key that we need to ignore after
a cin statement and it is ignored by
including the \n parameter.
- There is actually a bug in Visual C++ that forces users to input
an extra Enter key or two when getline is used in a program. See these
pages for details of this bug and a solution:
http://www.cs.drexel.edu/~mcs171/Sp03/extras/getline_Fixer/instructions.html
and
http://support.microsoft.com/default.aspx?scid=KB;EN-US;q240015
Objective #3: Understand the length function, string concatenation,
and relational operators with string variables.
- You can determine the current length of a string with the use of a
string's length function. For
example,
string name = "Bill";
int lengthOfName = 0;
lengthOfName = name.length( );
cout << "His name is " << lengthOfName <<
" letters long.";
- Do not forget to type the empty parentheses after the length
function.
- To compare two strings in order to see if they are exactly the same,
you can use the Boolean == operator.
For example,
if (string1 == string2)
{
cout << "The strings are the same!\n";
}
causes the message to be printed on the screen if the two strings, string1
and string2, are equivalent.
- You can also test two strings alphabetically (consult the ASCII table.)
For example,
if (string1 <
string2)
{
cout << "string1 comes before string2
alphabetically ";
}
- To combine two strings into one larger string is called concatenation.
To concatenate two string variables, you can use the concatenation
operator (+). For example,
name = "John";
lastName = "Doe";
cout << name
+ lastName;
would display the string literal "JohnDoe".
Objective #4: Use subscript notation to access individual characters
within a string variable.
- C++ numbers the individual characters in a string beginning with index
position 0. So after the declaration statement,
string stateName = "New Jersey";
is made, the 'N' is considered to be in the index position 0, the first
'e' in the index position 1, the 'w' in the index position 2, and so
on. Note that the index position 3 contains a blank space, which is,
of course, a valid character. A blank space has an ASCII value of 32.
- You may use subscript notation
to change the value of one specific character within a string. The assignment
statement,
stateName[1]
= 'E';
would change the lowercase e originally found in index
position 1 of the example above to an uppercase E. So the string is
now "NEw Jersey".