×

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions that can be utilised to do various jobs. In this tutorial, we will look at the String class's key characteristics.

The string class is part of the C++ library and provides significantly more capability than C-style strings. The length of a C++ string can be altered at runtime due to dynamic memory allocation, which is analogous to vectors. Because the string class is a container class, we can use an iterator to traverse over all of its characters, just like other containers like vector, set, and maps, but we usually use a simple for loop to iterate over the characters and index them using the [] operator.

The C++ string class includes a variety of functions for easily manipulating strings. The most useful ones are demonstrated in the code below.

C++ Program:

#include<iostream>
using namespace std;


int main () 
{
   string strng ( "This is a string" );
   cout << " String is: " << strng << endl;
   string strng2 ( strng );
   cout << " String is: " << strng2 << endl;
   string strng3 ( 5, 'F' );
   cout << " String is: "<< strng3 << endl;
   string strng4 ( strng, 5, 10 );
   cout << " String is: " << strng4 << endl;
   string strng5 ( strng.begin (), strng.begin () + 7 );
   cout << " String is: " << strng5 << endl;
}

Output:

String is: This is a string
String is: This is a string
String is: FFFFF
String is: is a strin
String is: This is

We'll now look at various String class operators. The fundamental operators are the assignment operator (=), the concatenate operator (+), and the index operator ([ ]). Let's look at some examples of these operators.

C++ Program:

#include<iostream>
using namespace std;


int main () {
   string strng = "Hi ";
   string strng2 = "Rohit";
   string strng3, strng4;
   strng3 = strng; //use assignment operator
   cout << "The value of str3: " << strng3 << endl;
   strng4 = strng + strng2; //concatenate two strings
   cout << "The value of str4: " << strng4 << endl;
   cout << "Character at position 1 of str: " << strng [1] << endl;
}

Output:

The value of str3: Hi
The value of str4: Hi Rohit
Character at position 1 of str: i

Finally, let's look at some string methods. These routines are used to perform key string-related tasks. Let's look at how the functions work in the example code.

C++ Program:

#include<bits/stdc++.h>
using namespace std;
int main () {
   string str1 = "Hello World!";
   cout << "The String before clearing : " << str1 << endl;
   str1.clear (); // clean the string
   cout << "The String after clearing : " << str1 << endl;
   str1 = "It is a string:";
   cout << "The length of the String using size() and length() functions: " << str1.size() << " and " << str1.length() << endl;
   cout << "The character at 1 position of the string is : " << str1.at(1) << endl;
   cout << "First character of the string is : " << str1.front() << endl;
   cout << "Last character of the string is : " << str1.back() << endl;
   cout << "The string to C like character array: " << str1.c_str() << endl;
   cout << "The string after appending some text in it : " << str1.append("ABCDEFGHIJ") << endl;
   string string2 = "IT IS AN ANOTHER STRING";
   cout << "The string after appending the text from string2 : " << str1.append(string2,0, 5) << endl;
   // We use the find function to find the substring of the string
   if ( str1.find("AN") != string::npos )
      cout << "\"AN\" is found in the string at " << str1.find("AN") << " position" <<endl;
   else
      cout << "substring not found in the string" << endl;
   cout << "The Substring of the length 3 from index 5: " <<str1.substr(5, 3) <<endl;
   cout << "The String after erasing the 4 characters from index 5: " <<str1.erase(5, 4) << endl;
   cout << "Replace the 7 characters from index 3: " <<str1.replace(3, 7, "C++Programming");
}

Output:

The String before clearing : Hello World!
The String after clearing : 
The length of the String using size() and length() functions: 15 and 15
The character at 1 position of the string is : t
First character of the string is : I
Last character of the string is : :
The string to C like character array: It is a string:
The string after appending some text in it : It is a string:ABCDEFGHIJ
The string after appending the text from string2 : It is a string:ABCDEFGHIJIT IS
substring not found in the string
The Substring of the length 3 from index 5:  a 
The String after erasing the 4 characters from index 5: It istring:ABCDEFGHIJIT IS
Replace the 7 characters from index 3: It C++Programming:ABCDEFGHIJIT IS

Related Topics

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Stringstream in C++ and its applications

In this tutorial, we will explore what the stringstream in C++ is. We will also learn its application. What is stringstream? With the aid of a stringstream, user can read from a...

2 minutes read.

What does Buffer Flush mean in C++

A buffer flush, to explain simple layman's terms, is nothing but the transfer of computer data which is being stored in a rentable temporary memory of your computer running either...

3 minutes read.

Observer Design Pattern in C++

The Observer design pattern is a behavioural design pattern that allows an object (known as the subject) to notify other objects (known as observers) when its state changes. This is...

3 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

std::distance() in C++

The primary function of std::distance is to facilitate the total number of elements if we have two iterators. It is defined inside the header files. It has both magnitude and...

2 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.

Program to find the GCD of two numbers in C++

Before understanding the program of GCD or HCF, one must know what GCD or HCF is. What is GCD? The GCD is referred to as Greatest Common Divisor. HCF is the other...

8 minutes read.

Find the Size of Array in C/C++ without using sizeof() function

We know that arrays in C/C++ are the most essential data structures as they have the ability to hold the data in a continuous manner line where the address of...

3 minutes read.

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ 1/2 decimal equal is 0.555555555555555555555 .... An indefinite number of lengths will require the storage...

4 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

C++ Enumeration

C++ Enumeration In C++, Enum is a special data type that contains some fixed sets of components that have various applications in the programming. Enum works fine with fixed constant sets...

3 minutes read.

Compile Time Polymorphism in C++

What is Polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. One application of polymorphism in...

4 minutes read.

Factorial of a Number in C++ using while Loop

What is a factorial? The factorial of a number is the product of all the positive numbers less than or equal to n, indicated by n! According to the standard for an...

6 minutes read.

Diamond Pattern using While-loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

5 minutes read.

Skyline Problem in C++

We have given n rectangular buildings in a 2-dimensional city. Here, to compute the Skyline of the given n rectangle structures in a two-dimensional metropolis while removing hidden lines, the...

3 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 minutes read.

C++ Aggregation

C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents...

4 minutes read.