Strings in C++
Hi, welcome to the series of data structure and algorithm made easy in this particular section we will about the string which is basically character array.
what is a string in C++?
A string is the stram of the character which allows a user to index every particular character the same as array allows to access its elements. example of strings are "also mentor", "developer", "coder".
How can we access all features of string in c++?
You can normally declare a string using "string" data type but if you want to access full feature of string you need to import the string library in C++.
How to import the string library in C++?
The string library can be imported as same as we import the iostream library. #include <cstring> we need to write the code in top upper lines of C++ codes. and we are good to go.
How can we write a string in character array?
There are two ways to define a character array of string in C++.
The fourth type of function in the string library is the operator function.
what is a string in C++?
A string is the stram of the character which allows a user to index every particular character the same as array allows to access its elements. example of strings are "also mentor", "developer", "coder".
How can we access all features of string in c++?
You can normally declare a string using "string" data type but if you want to access full feature of string you need to import the string library in C++.
How to import the string library in C++?
The string library can be imported as same as we import the iostream library. #include <cstring> we need to write the code in top upper lines of C++ codes. and we are good to go.
How can we write a string in character array?
There are two ways to define a character array of string in C++.
- char arrchar[5] = "hello";
- char arrchar[5] = {'h','e','l','l','o'};
What are the function of string in C++?
The first type of function is the Iterators function they iterate through the loop from string and get the desired output for us.
- begin:- this return iterator to beginning.
- end:- this return iterator to the ending.
- rbegin:- this return reverse iterator to reverse beginning.
- rend:- this return reverse iterator to reverse ending.
- cbegin:- this return const iterator to beginning.
- cend:- this return const iterator to the ending.
- crbegin:- this return reverse const iterator to reverse beginning.
- crend:- this return reverse const iterator to reverse ending.
The second type of function is the capacity function which deals with the capacity and size of string and string formatting.
- size:- return the length of the string.
- length:- return the length of the string.
- max_size:- return the maximum size of the string data type.
- resize:- this function resizes the string to a particular length.
- capacity:- return the size of the string occupied in memory.
- reserve:- request to change the capacity.
- clear:- clear the string at the send into null.
- empty:- check if a string is empty or not. (true/false).
The third type of function is access function.
- stringname[index]:- we can directly access the particular character by using their index of the element.
- at:- access particular character in the string at a particular position.
- back:- we can access the last character of a string.
- front:- we can access the front character of a string.
The fourth type of function in the string library is the operator function.
- copy:- this function copy content of first string to second string
- find:- this function finds a particular character in the string.
- rfind:- this function finds a particular character from the last of the string and returns the position.
- substr:- this function can access a particular substring from a string.
- compare:- this function compares the two strings and returns 1,0,-1.
The fifth type of function is string modifier function
- '+' operator:- use to concatenate two string.
- append:- add a second string at the end of the first string.
- push_back:- append a character at the end of the string.
- assign:- assign a value to the string.
- erase:- erase the character of the string.
- replace:- replace the current value of the string with another string.
- pop_back:- delete the last character of the string.
- swap:- swap content of two string with each other.
- getline:- the help to take input on a complete string from the user.
The six types of method are converter method in these methods we convert a string of numeric value to the corresponding integer, long, and double.
- stoi:- convert string to an integer.
- stol:- string to long.
- stoul:- string to unsigned long.
- stoll:- string to long long.
- stoull:- string to unsigned long long.
- stof:- string to float.
- stod:- string to double.
- stold:- string to long double.
There are a lot of functions in the string but don't worry we will learn string with very deep we do one practical program of c++ in which we use all the functions mentioned above so that we can see how strings actually work 😀.
Here is the source code of all strings inbuilt function.
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string str;
// using getline to take input
getline(cin,str);
cout<<"Input string is "<<str<<endl;
// using begin and end to print each charter
for ( string::iterator it=str.begin(); it!=str.end(); ++it)
{
cout <<*it;
}
cout<<endl;
for ( string::reverse_iterator it=str.rbegin(); it!=str.rend(); ++it)
{
cout <<*it;
}
cout<<endl;
cout<<"String size is "<<str.size()<<endl;
cout<<"String length is "<<str.length()<<endl;
cout<<"String max size is "<<str.max_size()<<endl;
cout<<"String capacity is "<<str.capacity()<<endl;
cout<<"String is empty "<<str.empty()<<endl;
// print character useing string charcter array index
for(int i=0;i<str.size();i++)
{
cout<<str[i];
}
cout<<endl;
// print all character using at function
for(int i=0;i<str.size();i++)
{
cout<<str.at(i);
}
cout<<endl;
string str2 ="the second string ";
str2.append(str);
cout<<str2<<endl;
str2.erase();
str2="new string for new work";
str.swap(str2);
cout<<"string one after swap "<<str<<endl;
cout<<"string 2 after swap "<<str2<<endl;
cout<<"sub string from index 0 to index 2 "<<str.substr(0,3)<<endl;
cout<<"comparision of two strings "<<str.compare(str2)<<endl;
// work only if your compiler support c++11.
string num = "111213";
cout<<"string to int "<<stoi(num)<<endl;
cout<<"string to long "<<stol(num)<<endl;
cout<<"string to long long "<<stoll(num)<<endl;
cout<<"string to double "<<stod(num)<<endl;
cout<<"string to float "<<stof(num)<<endl;
return 0;
}
#include<cstring>
using namespace std;
int main()
{
string str;
// using getline to take input
getline(cin,str);
cout<<"Input string is "<<str<<endl;
// using begin and end to print each charter
for ( string::iterator it=str.begin(); it!=str.end(); ++it)
{
cout <<*it;
}
cout<<endl;
for ( string::reverse_iterator it=str.rbegin(); it!=str.rend(); ++it)
{
cout <<*it;
}
cout<<endl;
cout<<"String size is "<<str.size()<<endl;
cout<<"String length is "<<str.length()<<endl;
cout<<"String max size is "<<str.max_size()<<endl;
cout<<"String capacity is "<<str.capacity()<<endl;
cout<<"String is empty "<<str.empty()<<endl;
// print character useing string charcter array index
for(int i=0;i<str.size();i++)
{
cout<<str[i];
}
cout<<endl;
// print all character using at function
for(int i=0;i<str.size();i++)
{
cout<<str.at(i);
}
cout<<endl;
string str2 ="the second string ";
str2.append(str);
cout<<str2<<endl;
str2.erase();
str2="new string for new work";
str.swap(str2);
cout<<"string one after swap "<<str<<endl;
cout<<"string 2 after swap "<<str2<<endl;
cout<<"sub string from index 0 to index 2 "<<str.substr(0,3)<<endl;
cout<<"comparision of two strings "<<str.compare(str2)<<endl;
// work only if your compiler support c++11.
string num = "111213";
cout<<"string to int "<<stoi(num)<<endl;
cout<<"string to long "<<stol(num)<<endl;
cout<<"string to long long "<<stoll(num)<<endl;
cout<<"string to double "<<stod(num)<<endl;
cout<<"string to float "<<stof(num)<<endl;
return 0;
}
I hope you like this tutorial of string in which we demonstrate the most common and daily useable string function so you don't need to write code from scratch you just need to import string library and you are ready to solve tricky string problem.
Thank you so much for reading this article I hope you like it as well and please don't forget to share with your friends 😃.
Hi if you want to write a post on our website then please check this article on How to do a guest post.
0 Comments