C++ String Class and its Applications
Last Updated :
21 Sep, 2022
C++ string class and its applications have more functions as discussed in this article String vs Character Array In C++, in addition to a character array, there exists a similar kind of way to implement string, that is using a string class which is a part of C++ standard library.
We need to add a header file to implement string using string class. The basic difference between a character array and a string is, in the case of a character array, the size has to be allotted at the time of declaration, i.e all memory once allocated is fixed and cannot be altered at run time. Whereas, for string, there is no need to specify the size and to allocate fixed memory at the time of declaration.
C++
// C++ Program to Demonstrate String Class
#include<iostream>
// for string class
#include<string>
using namespace std;
int main()
{
// Size has to be predefined in character array
char str[80] = "GeeksforGeeks";
// Size not predefined in string
string s("GeeksforGeeks");
// Printing character array and string
cout << str << endl;
cout << s << endl;
return 0;
}
OutputGeeksforGeeks
GeeksforGeeks
Some useful String Functions
1. compare(string_to_compare ):
It is used to compare two strings. It returns the difference between the second string and the first string in the integer.
C++
// C++ program to demonstrate use of compare()
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("GeeksforGeeks");
string str1("GeeksforGeeks");
// Comparing strings using compare()
if (str.compare(str1) == 0)
cout << "Strings are equal";
else
cout << "Strings are unequal";
return 0;
}
2. find(“string”):
Searches the string for the first occurrence of the substring specified in arguments. It returns the position of the first occurrence of substring.
3. find_first_of(“string”):
Searches the string for the first character that matches any of the characters specified in its arguments. It returns the position of the first character that matches.
4. find_last_of(“string”):
Searches the string for the last character that matches any of the characters specified in its arguments. It returns the position of the last character that matches.
5. rfind(“string”):
Searches the string for the last occurrence of the substring specified in arguments. It returns the position of the last occurrence of a substring
C++
// C++ program to demonstrate working of find(),
// rfind(),find_first_of() and find_last_of()
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str("The Geeks for Geeks");
// find() returns position to first
// occurrence of substring "Geeks"
cout << "First occurrence of \"Geeks\" starts from : ";
cout << str.find("Geeks") << endl;
// Prints position of first occurrence of
// any character of "reef" (Prints 2)
cout << "First occurrence of character from \"reef\" is at : ";
cout << str.find_first_of("reef") << endl;
// Prints position of last occurrence of
// any character of "reef" (Prints 16)
cout << "Last occurrence of character from \"reef\" is at : ";
cout << str.find_last_of("reef") << endl;
// rfind() returns position to last
// occurrence of substring "Geeks"
// Prints 14
cout << "Last occurrence of \"Geeks\" starts from : ";
cout << str.rfind("Geeks") << endl;
return 0;
}
OutputFirst occurrence of "Geeks" starts from : 4
First occurrence of character from "reef" is at : 2
Last occurrence of character from "reef" is at : 16
Last occurrence of "Geeks" starts from : 14
6. insert(pos_to_begin,string_to_insert):
This function inserts the given substring in the string. It takes two arguments, first the position from which you want to insert the substring and second the substring.
C++
// C++ program to demonstrate working of insert()
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str("Geeksfor");
// Printing the original string
cout << str << endl;
// Inserting "Geeks" at 8th index position
str.insert(8,"Geeks");
// Printing the modified string
// Prints "GeeksforGeeks"
cout << str << endl;
return 0;
}
OutputGeeksfor
GeeksforGeeks
7. clear():
This function clears all the characters from the string. The string becomes empty (length becomes 0) after this operation.
8. empty():
Tests whether the string is empty. This function returns a Boolean value.
C++
// C++ program to demonstrate working of clear()
// and empty()
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str("GeeksforGeeks");
// clearing string
str.clear();
// Checking if string is empty
(str.empty()==1)?
cout << "String is empty" << endl:
cout << "String is not empty" << endl;
return 0;
}
Similar Reads
C++ string class and its applications
In C++ we can store string by one of the two ways â C style stringsstring class (discussed in this post) In this post, the second method is discussed. string class is part of C++ library that supports a lot much functionality over C style strings. C++ string class internally uses char array to store
6 min read
stringstream in C++ and its Applications
A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). To use stringstream, we need to include sstream header file. The stringstream class is extremely useful in parsing input. Basic methods are:clear()- To clear the stream.str
3 min read
strchr() function in C++ and its applications
In C++, strchr() is a predefined function used for finding the occurrence of a character in a string. It is present in cstring header file. Syntax: // Returns pointer to the first occurrence// of c in str[]char *strchr(const char *str, int c) Note that c is passed as its int promotion, but it is int
4 min read
isupper() and islower() and their application in C++
In C++, isupper() and islower() are predefined functions used for string and character handling. cstring.h is the header file required for string functions and cctype.h is the headerfile required for character functions. isupper() Function: This function is used to check if the argument contains any
2 min read
std::string class in C++
C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.String vs Character ArrayStringChar
8 min read
std::string::append() in C++
The string::append() in C++ is used append the characters or the string at the end of the string. It is the member function of std::string class .The string::append() function can be used to do the following append operations:Table of ContentAppend a Whole StringAppend a Part of the StringAppend a C
3 min read
iscntrl() in C++ and its application to find control characters
In C++, iscntrl() is a predefined function used for string and character handling. cstring is the header file required for string functions and cctype is the header file required for character functions. A control character is one which is not a printable character i.e, it does not occupy a printing
3 min read
std::basic_string::at in C++
Returns a reference to the character at the specified location pos. The function automatically checks whether pos is the valid position of a character in the string (i.e., whether pos is less than the string length), throwing an out_of_range exception if it is not. Syntax: reference at (size_type po
1 min read
String Functions in C++
A string is referred to as an array of characters. In C++, a stream/sequence of characters is stored in a char array. C++ includes the std::string class that is used to represent strings. It is one of the most fundamental datatypes in C++ and it comes with a huge set of inbuilt functions. In this ar
8 min read
std::string::assign() in C++
The member function assign() is used for the assignments, it assigns a new value to the string, replacing its current contents. Syntax 1: Assign the value of string str. string& string::assign (const string& str) str : is the string to be assigned. Returns : *this CPP // CPP code for assign
5 min read