C++ Strings
C++ provides two types of string representations:
- C-style strings
- The string class type introduced by C++
C-style Strings
C-style strings originate from the C language and continue to be supported in C++. A string is essentially a one-dimensional array of characters terminated by a null character \0
. Therefore, a null-terminated string contains the characters that comprise the string followed by the null character.
The following declaration and initialization create a string tutorialpro. Since a null character is stored at the end of the array, the size of the character array is one more than the number of characters in the word tutorialpro.
char site[7] = {'R', 'U', 'N', 'O', 'O', 'B', '\0'};
According to the rules of array initialization, you can write the above statement as:
char site[] = "tutorialpro";
The following is the memory representation of a string defined in C/C++:
Actually, you do not need to place the null character at the end of a string constant. The C++ compiler automatically places \0
at the end of the string when it initializes the array. Let's try to print the above string:
Example
#include <iostream>
using namespace std;
int main ()
{
char site[7] = {'R', 'U', 'N', 'O', 'O', 'B', '\0'};
cout << "tutorialpro.org: ";
cout << site << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
tutorialpro.org: tutorialpro
C++ has a large set of functions for manipulating null-terminated strings:
No. | Function & Purpose |
---|---|
1 | strcpy(s1, s2); <br>Copies string s2 into string s1. |
2 | strcat(s1, s2); <br>Concatenates string s2 to the end of string s1. Concatenation can also be done with the + operator, for example: <br> string str1 = "tutorialpro";<br>string str2 = "google";<br>string str = str1 + str2; |
3 | strlen(s1); <br>Returns the length of string s1. |
4 | strcmp(s1, s2); <br>Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. |
5 | strchr(s1, ch); <br>Returns a pointer to the first occurrence of character ch in string s1. |
6 | strstr(s1, s2); <br>Returns a pointer to the first occurrence of string s2 in string s1. |
The following example uses some of these functions:
Example
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[13] = "tutorialpro";
char str2[13] = "google";
char str3[13];
int len ;
// Copy str1 into str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;
// Concatenate str1 and str2
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;
// Total length of str1 after concatenation
len = strlen(str1);
cout << "strlen(str1) : " << len << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
strcpy( str3, str1) : tutorialpro
strcat( str1, str2): tutorialprogoogle
strlen(str1) : 12
String Class in C++
The C++ Standard Library provides a string class type that supports all the operations mentioned above, and more. We will learn about the C++ Standard Library string class in this chapter. For now, let's look at the following example:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str1 = "tutorialpro";
string str2 = "google";
string str3;
int len ;
// Copy str1 into str3
str3 = str1;
cout << "str3 : " << str3 << endl;
// Concatenate str1 and str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
// Total length of str3 after concatenation
len = str3.size();
cout << "str3.size() : " << len << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
str3 : tutorialpro
str1 + str2 : tutorialprogoogle
str3.size() : 12
You might not fully understand this example yet, as we haven't discussed classes and objects so far. For now, you can just take a rough look at this example and come back to it after understanding the concepts of object-oriented programming.
Example
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str1 = "tutorialpro";
string str2 = "google";
string str3;
int len ;
// Copy str1 to str3
str3 = str1;
cout << "str3 : " << str3 << endl;
// Concatenate str1 and str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
// Total length of str3 after concatenation
len = str3.size();
cout << "str3.size() : " << len << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
str3 : tutorialpro
str1 + str2 : tutorialprogoogle
str3.size() : 12