We can still use all the functions present in C Language <string.h> library as including <cstring> and make use of all its string handling functions.
The string is an object of std::string class in c++. It provides many of the in-built functions that can be used with the strings without including any library.
These are some of the functions present in string class.
Function | Description |
int compare(const string& str) | It is used to compare two string objects. |
int length() | It is used to find the length of the string. |
void swap(string& str) | It is used to swap the values of two string objects. |
string substr(int pos,int n) | It creates a new string object of n characters. |
int size() | It returns the length of the string in terms of bytes. |
void resize(int n) | It is used to resize the length of the string up to n characters. |
string& replace(int pos,int len,string& str) | It replaces portion of the string that begins at character position pos and spans len characters. |
string& append(const string& str) | It adds new characters at the end of another string object. |
char& at(int pos) | It is used to access an individual character at specified position pos. |
int find(string& str,int pos,int n) | It is used to find the string specified in the parameter. |
int find_first_of(string& str,int pos,int n) | It is used to find the first occurrence of the specified sequence. |
int find_first_not_of(string& str,int pos,int n ) | It is used to search the string for the first character that does not match with any of the characters specified in the string. |
int find_last_of(string& str,int pos,int n) | It is used to search the string for the last character of specified sequence. |
int find_last_not_of(string& str,int pos) | It searches for the last character that does not match with the specified sequence. |
string& insert() | It inserts a new character before the character indicated by the position pos. |
int max_size() | It finds the maximum length of the string. |
void push_back(char ch) | It adds a new character ch at the end of the string. |
void pop_back() | It removes a last character of the string. |
string& assign() | It assigns new value to the string. |
int copy(string& str) | It copies the contents of string into another. |
char& back() | It returns the reference of last character. |
Iterator begin() | It returns the reference of first character. |
int capacity() | It returns the allocated space for the string. |
const_iterator cbegin() | It points to the first element of the string. |
const_iterator cend() | It points to the last element of the string. |
void clear() | It removes all the elements from the string. |
const_reverse_iterator crbegin() | It points to the last character of the string. |
const_char* data() | It copies the characters of string into an array. |
bool empty() | It checks whether the string is empty or not. |
string& erase() | It removes the characters as specified. |
char& front() | It returns a reference of the first character. |
string& operator+=() | It appends a new character at the end of the string. |
string& operator=() | It assigns a new value to the string. |
char operator[](pos) | It retrieves a character at specified position pos. |
int rfind() | It searches for the last occurrence of the string. |
iterator end() | It references the last character of the string. |
reverse_iterator rend() | It points to the first character of the string. |
void shrink_to_fit() | It reduces the capacity and makes it equal to the size of the string. |
char* c_str() | It returns pointer to an array that contains null terminated sequence of characters. |
const_reverse_iterator crend() | It references the first character of the string. |
reverse_iterator rbegin() | It reference the last character of the string. |
void reserve(inr len) | It requests a change in capacity. |
allocator_type get_allocator(); | It returns the allocated object associated with the string. |
In this article, we are going to learn about the string class functions and implement some of them with examples.
C++ String Compare():
This function compares the value of the string object to the sequence of characters specified by its parameter.
SYNTAX:
Suppose these are two strings and we would like to compare them. This function returns an integer value based on the comparison.
int k= str1.compare(str2);
- k==0 : If k contains value zero, it means both the strings are equal.
- k!=0 : If k does contain value zero, it means both the strings are unequal.
- k>0 : If k contains value more than zero, either the value of the first character is greater in the compared string or all the compared characters match but the compared string is longer.
- k<0 : If k contains value less than zero, either the value of the first character is lower in the compared string or all the compared characters match but the compared string is shorter.
Let us look at an example to implement this function:
#include<iostream>
using namespace std;
void main()
{
string str1="Hello";
string str2="inprogrammer";
int k= str1.compare(str2);
if(k==0)
cout<<"Both the strings are equal";
else
cout<<"Both the strings are unequal";
}
OUTPUT
Both the strings are not equal
C++ String length()
This function is used to find the length of the string in terms of bytes.
Syntax:
int len = s1.length();
This function returns the length of the string specified.
Let’s look at an example:
#include<iostream>
using namespace std;
int main()
{
string s1 = "Welcome to inprogrammer";
int len = s1.length();
cout<< "length of the string is : " << len;
return 0;
}
OUTPUT
Length of the string is 23
C++ string push_back():
This function is used to add one character at the end of the string and increases its length by one.
Let’s see an example:
#include<iostream>
using namespace std;
int main()
{
string s1 = "Hell";
cout<< "String is :" <<s1<<'\n';
s1.push_back('o');
cout<<"Now, string is :"<<s1;
return 0;
}
OUTPUT
String is Hell
Now, string is Hello
C++ string pop_back():
This function is used to remove one character from the end of the string and decreases its length by one.
Let’s see an example:
#include<iostream>
using namespace std;
int main()
{
string s1 = "Hello";
cout<< "String is :" <<s1<<'\n';
s1.pop_back();
cout<<"Now, string is :"<<s1;
return 0;
}
OUTPUT
String is Hello
Now, string is Hell
C++ String copy():
copy() function in c++ is used to copy the contents from one string to another.
Syntax
Source_string.copy(dest_string, length, position);
Let us look at an example:
#include<iostream>
using namespace std;
int main()
{
string str = "java programs";
char str1[13] ;
str.copy(str1,8,5);
str1[8] ='\0';
cout<<"String contains : " <<str1;
return 0;
}
OUTPUT
String contains programs
CONCLUSION:
Hurray! You have successfully learned the concepts of strings in c++. To master this concept, do some exercises on strings. Happy Coding 🙂