Basic features of string class for manipulating strings in C++

string manipulation using cpp

manipulating string objects using string classC++ does not support a built-in string type rather a string in C++ is a sequence of characters. Null terminated character arrays are used to store and manipulate strings. These strings are known as C-strings. C++ provides a new class called string which includes many constructors, member functions and operators. Here we will discuss the most important ones.

This is not a tutorial. If you want to revise the basic features of the string class in a few minutes then you have come to the right place. The program given below explains the important features of string class which are used for manipulating strings. The strings in the program are selected such that they can explain all the functions at ease. You should try the functions with other strings for better understanding.


#include<iostream>
#include<conio.h>
#include<string>

using namespace std;

int main(){
 string s1; //using constructor with no arguments
 s1="abc def ghi"; //assigning string objects
 string s2("123"); //using one argument constructor

//printing the original string objects
 cout<<"Original Strings are:\ns1="<<s1<<"\ns2="<<s2;

 //concatenating strings s1 and s2 using +
 string s3=s1+s2;
 cout<<"\n\ns3=s1+s2\n"<<s3;

 //concatenating strings using +=
 s3+=s2;
 cout<<"\n\ns3+=s2\n"<<s3;

 //manipulating string objects

 //insert a string into another
 s1.insert(3,s2); //inserting s2 into the 3rd position of s1
 cout<<"\n\nInserting s2 into the 3rd position of s1\n"<<s1;

 //replacing characters in a string
 s1.replace(1,3,s2); //replacing 3 characters starting from
 cout<<"\n\nReplacing 3 characters starting from the 1st position with s2\n"<<s1;
 //the 1st position with s2

 //remove characters from a string
 s1.erase(3,4); //removing 3 characters from the 3rd position
 cout<<"\n\nRemoving 4 characters from the 3rd position\n"<<s1;

 //relational operators on string objects

 //using !=, ==, < and > operators on strings
 cout<<"\n\nRelational operators\n";
 if(s1!=s2)
 cout<<"s1 is not equal to s2\n";

 if(s1==s2)
 cout<<"s2 is equal to s3\n";
 else if(s1>s2)
 cout<<"s1 is greater than s2\n";
 else
 cout<<"s2 is greater than s1\n";

 //characteristics of string objects like size, length, capacity, max size and empty

 cout<<"\n\nCharacteristics of string objects like size, length, capacity, max size and empty";
 cout<<"\nSize of s1: "<<s1.size(); //denotes the number of elements
 cout<<"\nLength of s1: "<<s1.length();//currently stored in the string
 cout<<"\nCapacity of s1: "<<s1.capacity(); //maximum elements that can be stored
 cout<<"\nMaximum Size: "<<s1.max_size(); //largest possible size of string object
 cout<<"\nEmpty: "<<s1.empty(); //is the string empty

 //accessing characters in strings

 cout<<"\n\nAccessing characters in strings:\n";
 cout<<"Element at 7th postion: "<<s1.at(7); //displays the character at 1st position
 cout<<"\nPosition of \"ghi\" in s1: "<<s1.find("ghi"); //finds the string "ghi"
 cout<<"\nSubstring of 3 from 7th position: "<<s1.substr(7,3); //retrieve the substring

 //comparing and swapping

 cout<<"\n\nComparing ";
 cout<<"\ns1.compare(s2): "<<s1.compare(s2); //it returns >0 when s1>s2 and vice-versa
 cout<<"\ns2.compare(s2): "<<s2.compare(s2); //it returns 0 when the strings are equal
 cout<<"\n\nSwapping:";
 cout<<"\nBefore Swapping";
 cout<<"\ns1: "<<s1;
 cout<<"\ns2: "<<s2;
 s1.swap(s2);
 cout<<"\nAfter Swapping: ";
 cout<<"\ns1: "<<s1;
 cout<<"\ns2: "<<s2;
 getch();
 return 0;
}

Here I have covered most of the basic features of the string class. There are plenty of other features in the string class which you might find interesting. If you have any questions regarding the post please use the comments below. 😀