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++
#include<iostream>
#include<string>
using namespace std;
int main()
{
char str[80] = "GeeksforGeeks" ;
string s( "GeeksforGeeks" );
cout << str << endl;
cout << s << endl;
return 0;
}
|
Output
GeeksforGeeks
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++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str( "GeeksforGeeks" );
string str1( "GeeksforGeeks" );
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++
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str( "The Geeks for Geeks" );
cout << "First occurrence of \"Geeks\" starts from : " ;
cout << str.find( "Geeks" ) << endl;
cout << "First occurrence of character from \"reef\" is at : " ;
cout << str.find_first_of( "reef" ) << endl;
cout << "Last occurrence of character from \"reef\" is at : " ;
cout << str.find_last_of( "reef" ) << endl;
cout << "Last occurrence of \"Geeks\" starts from : " ;
cout << str.rfind( "Geeks" ) << endl;
return 0;
}
|
Output
First 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++
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str( "Geeksfor" );
cout << str << endl;
str.insert(8, "Geeks" );
cout << str << endl;
return 0;
}
|
Output
Geeksfor
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++
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str( "GeeksforGeeks" );
str.clear();
(str.empty()==1)?
cout << "String is empty" << endl:
cout << "String is not empty" << endl;
return 0;
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...