×

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

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

C++ Call by Reference

Call by Reference is a C++ method for passing arguments to a function that enables us to pass the actual memory address of the parameter rather than a copy of...

4 minutes read.

Backtracking Time Complexity in C++

Introduction to Backtracking Backtracking is an important part of programming languages, and with the help of backtracking, we can do many operations in an advanced data structure. For doing major operations,...

4 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

C++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

C++ 11 vs C++ 14 vs C++ 17

C++ is a language that is used to create a high-performance application. C++ 11, C++ 14, and C++ 17 are the different version of C++. There are some differences between...

1 minute read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

C++ Inline function

A C++ function that extends in line when called is known as an inline function. It reduces function call overhead by having the compiler use the function code rather than...

4 minutes read.

4 Pillars of OOPs

OOPs OOPS means Object Oriented Programming System Structure. Object oriented Programming is defined as an approach that provides a way of modularizing programs by creating partitioned memory area for both data...

11 minutes read.

C++ Deque

Definition: Deque or the Doubly ended queue is a data structure or operation performed under queue where insertion and deletion are allowed at both ends. A deque is an ordered collection of...

5 minutes read.

Armstrong number using for loop in C++

What is For Loop? A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax that can be...

4 minutes read.

C++ Constructor

Constructor is a specific method in C ++ that is automatically called when an object is created. Typically, it is used to set the data members of a new object....

4 minutes read.

Virtual Functions and Runtime Polymorphism in C++

In this tutorial, we will explore more on virtual functions and runtime polymorphism in the most useful language C++. A virtual function is a member function with the keyword virtual used...

6 minutes read.

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide...

2 minutes read.

Difference between exit() and _Exit() in C++

Before understanding the difference between the exit() and _Exit(), one must know about exit() and _Exit() functions. The exit() function in C/C++ The exit() method in the C language kills the calling...

3 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.

C++ operator

In this article, we will discuss about the operators in C++ with their types and examples. An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains...

5 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.