C++ string class and its applications?


The C++ has the String class. That is different than the traditional C strings. The C string is actually the character array. In C++, the string class has few different properties. It has different functions, that can be used to perform different tasks. Here we will see the important features of the String class.

In the first section we will see how the constructors of the string class works in different way. Let us see by example.

Example

#include<iostream>
using namespace std;
int main() {
   string str("This is a string");
   cout << "String is: " << str << endl;
   string str2(str); // initialization by another string str
   cout << "String is: " << str2 << endl;
   string str3(5, 'A'); //initialize by a character
   cout << "String is: "<< str3 << endl;
   string str4(str, 5, 10); //initialize using another string from index 5 to index 10
   cout << "String is: " << str4 << endl;
   string str5(str.begin(), str.begin() + 7); //initialize using str from first to index 7
   cout << "String is: " << str5 << endl;
}

Output

String is: This is a string
String is: This is a string
String is: AAAAA
String is: is a strin
String is: This is

Now let us discuss about some operators of String class. The basic operators are Assignment operator(=), Concatenate operator (+), Index operator ([ ]). Let us see the examples of these operators.

Example

#include<iostream>
using namespace std;
int main() {
   string str = "Hello ";
   string str2 = "World";
   string str3, str4;
   str3 = str; //use assignment operator
   cout << "The value of str3: " << str3 << endl;
   str4 = str + str2; //concatenate two strings
   cout << "The value of str4: " << str4 << endl;
   cout << "Character at position 1 of str: " << str[1] << endl;
}

Output

The value of str3: Hello
The value of str4: Hello World
Character at position 1 of str: e

Finally set us see some string functions. These functions are used to do some important task related to strings. Let us see the functions in action in the example code

Example

#include<iostream>
using namespace std;
int main() {
   string str = "Hello";
   cout << "String before clear: " << str << endl;
   str.clear(); //clean the string
   cout << "String after clear: " << str << endl;
   str = "This is a string:";
   cout << "String length using length() and size() functions: " <<str.length() << " and " << str.size() << endl;
   cout << "Character at position 1 of str: " << str.at(1) << endl;
   cout << "First character of str: " << str.front() << endl;
   cout << "Last character of str: " << str.back() << endl;
   cout << "String to C like character array: " << str.c_str() << endl;
   cout << "String after appending text : " << str.append("ABCDEF") << endl;
   string str2 = "ANOTHER STRING";
   cout << "String after appending text from str2 : " << str.append(str2,0, 5) << endl;
   //find function to find substring
   if (str.find("is") != string::npos)
      cout << "\"is\" is found in str at " << str.find("is") << " pos" <<endl;
   else
      cout << "substring not found in str" << endl;
   cout << "Substring of length 3 from index 5: " <<str.substr(5, 3) <<endl;
   cout << "String after erasing 4 characters from index 5: " <<str.erase(5, 4) << endl;
   cout << "Replace 7 characters from index 3: " <<str.replace(3, 7, "C++Programming");
}

Output

String before clear: Hello
String after clear:
String length using length() and size() functions: 17 and 17
Character at position 1 of str: h
First character of str: T
Last character of str: :
String to C like character array: This is a string:
String after appending text : This is a string:ABCDEF
String after appending text from str2 : This is a string:ABCDEFANOTH
"is" is found in str at 2 pos
Substring of length 3 from index 5: is
String after erasing 4 characters from index 5: This
string:ABCDEFANOTH
Replace 7 characters from index 3: ThiC++ Programmingng:ABCDEFANOTH

Updated on: 31-Jul-2019

90 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements