ANSI C++ now has a string type (earlier versions of C++ had to use
an array of char to get the same effect, these are now known as C
strings). string header file must be included.
//declare string variables
string firstName, lastName;
//input and output strings
cout << "Enter your first and last names: ";
cin >> firstName >> lastName;
cout << "Welcome " << firstName << ' ' << lastName << endl;
//compare strings with relational operators
if (firstName == lastName)
cout << "Your first and last names are the same!?";
else if (firstName < lastName)
cout << "Your firstname comes before your last name in dictionary order";
//assign one string to another
code = lastName;
//a string knows its length (number of characters it is)
cout << "length of your last name: " << lastName.length();
//string is a class, variable of a class is an "object", objects have functions
//that are accessed by this . notation.
//string has many other functions.
©2000 David Wills