C++ Strings
Strings are sequence of characters. C++ supports two ways of representing strings :
1) C style string ( Array of characters )
2) Standard string class present in C++ library
C style strings
C language supports string representation with array of characters terminated by a null character ‘ \0 ‘. This feature remained a part of C++ language as well.
Consider the following statements to initialize a string (character array) :
char str [ ] = { ‘W’, ‘O’, ‘R’, ‘L’, ‘D’, ‘\0’ }; // Initializing a NULL terminated string ‘WORLD’ char str [ ] = ” WORLD “ ; // Another way of initializing |
Please note that the second statement doesn’t contain ‘ \0 ‘ since in this case, NULL character is automatically appended by the C++ compiler.
Standard string manipulation function
The header file < cstring > provides a number of standard functions which can be used for string manipulation. Some of the most commonly used functions are :
Function | Purpose |
strlen ( str1 ) | Returns the length of string str1 |
strcpy ( str1, str2 ) | Copies string str1 into str2 |
strcat ( str1, str2 ) | Appends string str2 at the end of string str1 |
strcmp ( str1, str2 ) | Compares string str1 and str2 character by character and returns 0 if they are equal, else returns the difference of ascii values of the first mismatching characters of the two strings. |
There are a lot of standard functions offered by the library. Following program demonstrates the use of some string manipulation functions :
#include<iostream> #include<cstring> using namespace std; int main() { char str1[10] = { 'G', 'o', 'o', 'd', '\0' }; char str2[] = "Bye"; char str3[10]; // declare a string to store maximum 10 characters int len = strlen(str1); // get the length of str1 cout << "Length of str1 is " << len << endl; cout << "str1 : " << str1 << endl; cout << "str2 : " << str2 << endl; strcpy(str3, str2); // copy str2 into str3 cout << "str3 : " << str3 << endl; if (strcmp(str2, str3) == 0) { // compare two strings cout << "Two strings are equal" << endl; } else { cout << "Strings are not equal" << endl; } strcat(str1, str2); // concatenate/append str2 to str1 cout << "After concatenating, str1 : " << str1 << endl; return 0; }
Taking string as user input
Strings can be taken as user input from console using following ways :
cin >> str; | Stops taking the input when it sees a space character. Also it is dangerous since there is no length check. |
gets(str); | Dangerous to use this because it doesn’t check for length of the input string. This may lead to buffer overflow ( Suppose the string is declared as char str [ 10 ] and user enters a string of length > 10 ). |
fgets(str, 10, stdin); | Safer form of gets as it contains a length parameter. |
cin.getline(str, 10); | Yet another way to input a string in C++. |
Following program demonstrates the safe techniques to take string as input from console :
#include<iostream> #include<cstdio> using namespace std; int main() { char str1[10], str2[10]; // can store strings of max length 10 cout << "Enter a string : "; fgets(str1, 10, stdin); // input a string of size 10 cout << "Enter another string : "; cin.getline(str2, 10); // another way of taking input cout << "str1 : " << str1; cout << "str2 : " << str2 << endl; return 0; }
string class in C++
The C++ library < string > provides a rich set of string manipulation functions. Now, we can use string as a data type like int, float, etc. and we can declare a variable of type string. Following program demonstrates the use of string data type and some of the member functions of string class :
#include<iostream> #include<string> using namespace std; int main() { string str1 = "civic"; string str2 = "subject"; string str3; str3 = str2; // copy str2 to str3 str2 = str1 + str3; // concatenate str1 and str3 cout << "str1 : " << str1 << endl; cout << "str2 : " << str2 << endl; cout << "str3 : " << str3 << endl; str1.push_back('s'); // append a character to a string cout << "After Append str1 : " << str1 << endl; int len = str2.length(); // get the length of str2 cout << "Length of str2 : " << len << endl; str3.append(str1); // append string str1 at the end of str3 cout << "After append str3 : " << str3 << endl; string str4 = str3.substr(3); // get the substring starting from index 3 cout << "Substring str4 : " << str4 << endl; return 0; }