C++ string class and its applications
In C++ we can store string by one of the two ways –
- C style strings
- string 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 character but all memory management, allocation, and null termination is handled by string class itself that is why it is easy to use. The length of the C++ string can be changed at runtime because of dynamic allocation of memory similar to vectors. As string class is a container class, we can iterate over all its characters using an iterator similar to other containers like vector, set and maps, but generally, we use a simple for loop for iterating over the characters and index them using the [] operator. C++ string class has a lot of functions to handle string easily. Most useful of them are demonstrated in below code.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str1("first string");
string str2(str1);
string str3(5, '#' );
string str4(str1, 6, 6);
string str5(str2.begin(), str2.begin() + 5);
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << str4 << endl;
cout << str5 << endl;
string str6 = str4;
str4.clear();
int len = str6.length();
cout << "Length of string is : " << len << endl;
char ch = str6.at(2);
cout << "third character of string is : " << ch << endl;
char ch_f = str6.front();
char ch_b = str6.back();
cout << "First char is : " << ch_f << ", Last char is : "
<< ch_b << endl;
const char * charstr = str6.c_str();
printf ("%s\n", charstr);
str6.append(" extension");
str4.append(str6, 0, 6);
cout << str6 << endl;
cout << str4 << endl;
if (str6.find(str4) != string::npos)
cout << "str4 found in str6 at " << str6.find(str4)
<< " pos" << endl;
else
cout << "str4 not found in str6" << endl;
cout << str6.substr(7, 3) << endl;
cout << str6.substr(7) << endl;
str6.erase(7, 4);
cout << str6 << endl;
str6.erase(str6.begin() + 5, str6.end() - 3);
cout << str6 << endl;
str6 = "This is a examples";
str6.replace(2, 7, "ese are test");
cout << str6 << endl;
return 0;
}
|
Output :
first string
first string
#####
string
first
Length of string is : 6
third character of string is : r
First char is : s, Last char is : g
string
string extension
string
str4 found in str6 at 0 pos
ext
extension
string nsion
strinion
These are examples
As seen in the above code, we can get the length of the string by size() as well as length() but length() is preferred for strings. We can concat a string to another string by += or by append(), but += is slightly slower than append() because each time + is called a new string (creation of new buffer) is made which is returned that is a bit overhead in case of many append operation.
Applications : On basis of above string function some application are written below :
CPP
#include <bits/stdc++.h>
using namespace std;
string returnFloatingPart(string str)
{
int pos = str.find(".");
if (pos == string::npos)
return "";
else
return str.substr(pos + 1);
}
bool containsOnlyDigit(string str)
{
int l = str.length();
for ( int i = 0; i < l; i++)
{
if (str.at(i) < '0' || str.at(i) > '9' )
return false ;
}
return true ;
}
string replaceBlankWith20(string str)
{
string replaceby = "%20";
int n = 0;
while ((n = str.find(" ", n)) != string::npos )
{
str.replace(n, 1, replaceby);
n += replaceby.length();
}
return str;
}
int main()
{
string fnum = "23.342";
cout << "Floating part is : " << returnFloatingPart(fnum)
<< endl;
string num = "3452";
if (containsOnlyDigit(num))
cout << "string contains only digit" << endl;
string urlex = "google com in";
cout << replaceBlankWith20(urlex) << endl;
return 0;
}
|
Output :
Floating part is : 342
string contains only digit
google%20com%20in
Time Complexity : O(N), here N is length of string .
Space Complexity : O(1) , since we not used any extra space.
Related Articles:
Last Updated :
14 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...