C++ has a string class.
You can use a type string variable.
To use the string class, a program has to include the string header file.
The string class is part of the std namespace.
The class lets you treat a string much like an ordinary variable.
The following code illustrates some of the similarities and differences between string objects and character arrays.
#include <iostream>
#include <string> // make string class available
using namespace std;
int main() { //from w w w .j a va 2 s. c o m
char my_char1[20]; // create an empty array
char my_char2[20] = "C++"; // create an initialized array
string str1; // create an empty string object
string str2 = "Java"; // create an initialized string
cout << "Enter a string: ";
cin >> my_char1;
cout << "Enter another string: ";
cin >> str1; // use cin for input
cout << my_char1 << " " << my_char2 << " "
<< str1 << " " << str2 // use cout for output
<< endl;
cout << "The third letter in " << my_char2 << " is "
<< my_char2[2] << endl;
cout << "The third letter in " << str2 << " is "
<< str2[2] << endl; // use array notation
return 0;
}
The code above generates the following result.
The following code shows some String usages.
Note that you can add and append C-style strings as well as string objects to a string object.
#include <iostream>
#include <string> // make string class available
using namespace std;
int main() { /*from www . ja v a 2 s.c o m*/
string s1 = "java2s.com";
string s2, s3;
cout << "You can assign one string object to another: s2 = s1\n";
s2 = s1;
cout << "s1: " << s1 << ", s2: " << s2 << endl;
cout << "You can assign a C-style string to a string object.\n";
cout << "s2 = \"new Value\"\n";
s2 = "C++";
cout << "s2: " << s2 << endl;
cout << "You can concatenate strings: s3 = s1 + s2\n";
s3 = s1 + s2;
cout << "s3: " << s3 << endl;
cout << "You can append strings.\n";
s1 += s2;
cout <<"s1 += s2 yields s1 = " << s1 << endl;
s2 += " for a day";
cout <<"s2 += \" for a day\" yields s2 = " << s2 << endl;
return 0;
}
The code above generates the following result.
C library equivalent of
str3 = str1 + str2;
is this:
strcpy(my_char3, my_char1); strcat(my_char3, my_char2);
The following code compares techniques used with string objects with techniques used with character arrays.
#include <iostream>
#include <string> // make string class available
#include <cstring> // C-style string library
using namespace std;
int main() { /*from ww w . ja va 2 s .c om*/
char my_char1[20];
char my_char2[20] = "java2s.com";
string str1;
string str2 = "C++";
str1 = str2; // copy str2 to str1
strcpy(my_char1, my_char2); // copy my_char2 to my_char1
str1 += " vs "; // add to the end of str1
strcat(my_char1, " Java"); // add to the end of my_char1
int len1 = str1.size(); // obtain length of str1
int len2 = strlen(my_char1);// obtain length of my_char1
cout << "The string " << str1 << " contains "
<< len1 << " characters.\n";
cout << "The string " << my_char1 << " contains "
<< len2 << " characters.\n";
return 0;
}
The code above generates the following result.
You can use cin with the >> operator to read a string object and cout with the << operator to display a string object using the same syntax you use with a C-style string.
Reading a line at a time instead of a word at time uses a different syntax.
#include <iostream>
#include <string> // make string class available
#include <cstring> // C-style string library
using namespace std;
int main() { /*from ww w . j av a 2 s. c om*/
char my_char[20];
string str;
cout << "Length of string: "
<< strlen(my_char) << endl;
cout << "Length of string in str before input: "
<< str.size() << endl;
cout << "Enter a line of text:\n";
cin.getline(my_char, 20); // indicate maximum length
cout << "You entered: " << my_char << endl;
cout << "Enter another line of text:\n";
getline(cin, str); // cin now an argument; no length specifier
cout << "You entered: " << str << endl;
cout << "Length of string in my_char after input: "
<< strlen(my_char) << endl;
cout << "Length of string in str after input: "
<< str.size() << endl;
return 0;
}
The code above generates the following result.