Latest Code Tutorials

C++ String Class Example | String Class in C++ Tutorial


C++ String Class is referred to as std:: string. The string class provides us the features to store the characters as a series of bytes which makes us access a single byte of the character taken into account. C++ is an object-oriented language, allows us to declare a series of characters using an object of inbuilt “String” class. This class 

C++ String Class

String class stores the characters as a sequence of bytes with the functionality of allowing access to a single-byte character. If we want to use the String Class, <string>(String Header) has to include necessarily.

#Declare and initialize a String

String str;
str = ”Hello World”;

Here String is the name of Class, and the str is the object of class String.

#Difference Between String And Character Array


          Character Array


1. Memory is allocated statically. So there is a wastage of memory. As allocated at compile-time. 

1. Memory is allocated dynamically So there is no wastage of memory as allocated at run-time.

2. Operators in C++ can’t be applied to a character array.


2. Standard C++ Operator can be applied to String.

3. In the case of the character, array Implementation is faster.     

3. In the case of Strings, there is Slower implementation.

4. char array-name [size];4. string string-name;

5. Character array is a collection of variables of the character data type.

5. The string is class, and variables of the string are the objects of class “string.”


#Operations on String

Let’s see the different operations of String in C++.

#Length of the String

The length( ) uses to calculate the number of characters in that string.

int len;
string str = "Hello World";

This len stores the length of string str and output will be the length of the string str.

See the following output.


#Compare two strings

Values in two strings can be checked for equality using compare( ) function  Or “==.”

string str1 = "App";
string str2 = "Dividend";
 if (str1 == str2) //or we can use  if ( == 0)
      cout << "Equal";
      cout << "Not Equal";

See the following output.

Not Equal

#Add a character at the end

The push_back( ) function is used to insert a character at the end of the string. It requires only one, the string or character you want to insert.


It will add ‘a’ character at the end of the str.

#Delete a character at the end

The pop_back( ) function is used to delete a character at the end of the string.


#Change the size of the string

The resize( ) function is used to increase or decrease the size of the string str. The parameter you need to give is the new size of the string.


See the following example.


Makes the size of the string str 13.

#Appending Two Strings

Two string can be appended by using the ‘+’ or the append( ) function.

string str1 = "App";
string str2 = "Dividend";
string str3 = str1 +" "+ str2; //or we can use str1.append(str2);

The output of str3 will be App Dividend

#Swapping of two strings

Two strings can be swapped by using swap( ) function. See the following syntax.


See the following example.

string str1 = "App";
string str2 = "Dividend";

#Reversing the iterator

  1. rbegin( ): used to return a reverse iterator pointing at the end of the string.
  2. rend( ): used to return a reverse iterator pointing at the beginning of the string.


for (st=str.rbegin(); st!=str.rend(); st++) 

#Know the capacity

The capacity( ) function is used to know the capacity taken by the string. It may or may not be equal to the size of the string. See the following syntax.


#Using the substring

This function is used to store the substring of a string.

See the following syntax.

substr (start index,end index);

This function takes two parameters,

Here start index is the index from where we want to start substring and end index is the last of the substring.

int main()
       string str= "AppDividend";
       string sub= str.substr(1,4);
       cout << "String: " << sub;

See the output.


#Replacing the content

There is a function called assign() in C++ used to assign a new value to string, and its current value has been replaced.

See the following syntax.


See the following code example.

int main( )

This will make str1 be Dividend. So in this way we have replaced the content of str1. See the following output.


#Maximum Size of String

The max_size( ) tells us the maximum size that the string can reach.

See the following syntax.


It is the maximum potential length.

#Erase the content of the String

The clear( ) function will erase all the content stored in the string. See the following syntax.

string_name.clear( );


It helps to reduce the capacity of the string to fit its size without affecting the length of the string. See the following code example.

int main ()
  string str (1000,'x');

This will make the capacity 10 of the string.

#Find in the string

It searches for the first occurrence of the argument in the string. See the following syntax.


See the following code example.

string s="Hey you, how are you";
int loc=s.find("you");
cout<<"The first occurrence of 'you' is: "<<loc;

#Get character in the string

The helps to get the character present at the i index of the string. See the following example.

int main ( )
  string str ("App Dividend");
  cout <<;

See the following output.


Hence it helps us to get the character present at the 2nd index of the string.

#Append the string

It makes the string to extend by inserting the string/characters at the end. It can be done with the help of operator +=. See the following example.

int main{
  string name ("App");
  string surname("Dividend");
  name += surname;   

See the output.


Finally, C++ String Class Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.