String Member Functions in C++

We covered the fundamentals of String Class in C++ in our previous post. In this post, we are going to cover the several operations and functionalities that we can perform on the String type. There are several member functions that are available in string class in C++ which is veryful for solving string related coding problems. 

Here we are going to discuss all the string member functions available in C++ programming language with real example and code implementation.

String empty() function in C++.

String Function Definition
empty() This function is used to check if a string is empty or not. It returns 1 if a string is empty else returns 0 if a string is not empty.
Example C++ Code:
/*C++ program to show the working of
empty() function
*/
#include<iostream>
#include<string>
using namespace std;

int main(){
    
    string str;
    //taking input from user
    cout<<"Enter a string: ";
    getline(cin, str);
    //checking string is empty or not
    if(!str.empty()){
        cout<<"The string is: "<<str<<endl;
    }
    else{
        cout<<"The string is empty!"<<endl;
    }
    
    return 0;
}
Output:
Enter a string: 
The string is empty!
Enter a string: Algolesson
The string is: Algolesson
In the output of the above code, you can see that when I enter nothing as an input, the empty() function checks the string and executes the else part saying, "The string is empty!". You have also noticed one getline() function that takes input from the user.


String Modifier functions in C++.

String Function Definition
push_back() This function is used to add a character at the end of the string.
pop_back() This function is used to delete the last character from the string.
insert() This function is used to insert additional characters into the string.
erase() This function is used to remove the part of the string which reduce the size of the string.
Example C++ Code:
/*C++ program to show the working of erase()
insert(), push_back() and pop_back() function
*/
#include<iostream>
#include<string>
using namespace std;

int main(){
    
    string str;
    //taking input from the user
    cout<<"Enter a string: ";
    getline(cin, str);
    
    cout<<"The string is: "<<str<<endl;
    //adding a char at the end of the string
    str.push_back('s');
    cout<<"String after push_back() operation: "<<str<<endl;
    //removing a char from the end of the string
    str.pop_back();
    cout<<"String after pop_back() operation: "<<str<<endl;

    string str1 = "Rose is a ";
    str.insert(0, str1);
    cout<<"String after insert operation: "<<str<<endl;

    str.erase(10, 10);
    cout<<"String after erase operation: "<<str<<endl;
return 0; }
Output:
Enter a string: Beautiful Flower
The string is: Beautiful Flower
String after push_back() operation: Beautiful Flowers
String after pop_back() operation: Beautiful Flower
String after insert operation: Rose is a Beautiful Flower
String after erase operation: Rose is a Flower
If you are already working with string then you probably know the functionality of getline() function. In the above code, push_back('s') adds one s character at the end of our string and the pop_back() function removes that end 's' character.

String Capacity functions in C++. 

String Function Definition
capacity() This function is used to return the capacity allocated to the string which can be equal to more than the length of the string.
length() This function is used to check the length of the string.
resize() This function is used to change the size of the current string. The size of the string can be increased or decreased based on demand.
shrink_to_fit() This function is used to decrease the capacity of the string to fit its size. It makes string capacity equal to string size.
size() This function is used to find the length of the string which also indicates the number of characters present in the string.
  • The size() functional and length() function return the same value always.
Example C++ Code:
/*C++ program to show the working of resize(),
capacity() and shrink_to_fit() function
*/
#include<iostream>
#include<string>
using namespace std;

int main(){
    
    string str;
    //taking input from user
    cout<<"Enter a string: ";
    getline(cin, str);
    
    cout<<"The string is: "<<str<<endl;

    str.resize(7);
    cout<<"String after resize operation: "<<str<<endl;

    cout<<"The capacity of the string is: "<<str.capacity()<<endl;

    cout<<"The length of the string is: "<<str.length()<<endl;

    str.shrink_to_fit();
    cout<<"The capacity of the string after shrink: "<<str.capacity()<<endl;

    cout<<"The size of the string is: "<<str.size()<<endl;
return 0; }
Output:
Enter a string: Welcome to Algolesson
The string is: Welcome to Algolesson
String after resize operation: Welcome
The capacity of the string is: 30
The length of the string is: 7
The capacity of the string after shrink: 15
The size of the string is: 7
In the above, code you can see that number of characters present in the input string was 21 and after resizing the string to 7 it contains only 7 characters but the capacity of the string was still 30 and even tho the length of the string showing is 7 so after performing shrink operation to free unwanted memory the capacity reduced to 15. 

String Iterator Functions in C++.

String Function Definition
begin() This function is used to return an iterator to the beginning of the string.
end() This function is used to return an iterator to the end of the string.
rbegin() This function is used to return a reverse iterator pointing at the end of the string.
rend() This function is used to return a reverse iterator pointing at the beginning of the string.
Example C++ Code:
/*C++ program to show the working of begin(), end()
rbegin() and rend() function
*/
#include<iostream>
#include<string>
using namespace std;

int main(){
    
    string str = "Algolesson";
    
    std::string::iterator it;

    cout<<"Print String using forward iterator: ";
    for(it = str.begin(); it != str.end(); it++)
       cout<<*it;
    cout<<endl;

    std::string::reverse_iterator rit;

    cout<<"Print String using reverse iterator: ";
    for(rit = str.rbegin(); rit != str.rend(); rit++)
       cout<<*rit;   
       
    return 0;
}
Output:
Print String using forward iterator: Algolesson
Print String using reverse iterator: nosseloglA
In the above code, when we use the reverse iterator to print our string then it prints the string in reverse order because begin() points to the end of the string to it start printing in reverse order.

String Operation Functions in C++.

String Function Definition
copy() This function is used to copy a sequence of characters from the string. This function returns a char array and the function required three arguments, the name of the char array, the length of the string to be copied, and the starting position of copying. 
find() This function is used to find content in the string. It can contain two arguments,
  • First argument is the string to be searched for.
  • Second argument is the starting index at which the search is going to start.
substr() This function is used to generate a substring of the string. It contain two argument,
  • First argument is the position of first character to be copied as a substring.
  • Second argument tell us about the length of the substring.
compare() This function is used to compare two strings and return 0 if both are equal.
Example C++ Code:
/*C++ program to show the working of copy(), find()
substr() and compare() function
*/
#include<bits/stdc++.h>
#include<iostream>
#include<string>
using namespace std;

int main(){

  string str = "Coding is Fun";
  string str1 = "Programming is Fun Coding is Fun";
  string str2 = "Fun";
  
  //char array of size 20
  char copystr[20];
  int length = str1.copy(copystr, 11, 0);
  copystr[length] = '\0';
  cout<<"The copied character array is: "<<copystr<<endl;

  int found = str1.find(str2);
  cout<<"The First occurrence of str2 at: "<<found<<endl;

  found = str1.find(str2, found+1);
  cout<<"The Second occurrence of str2 at: "<<found<<endl;

  string str3 = str1.substr(19, 6);
  cout<<"The First substring is: "<<str3<<endl;

  str3 = str1.substr(19);
  cout<<"The Second substring is: "<<str3<<endl;

  if(str.compare(str3) == 0)
    cout<<"Both strings are equal."<<endl;
     
  return 0;
}
Output:
The copied character array is: Programming
The first occurrence of str2 at: 15
The Second occurrence of str2 at: 29
The First substring is: Coding
The Second substring is: Coding is Fun
Both strings are equal.
In the above code, the copy() function return the character array and with null character at the end so we need to append it sepeararately or it might cause unexpected error. The substr() function help to capture substring and usually required two argument (starting position and size of substring) but if you don't provide the size of substring then it will capture from starting position till end of the string.

I hope you found this article useful, please share your feedback in the comment section below so we can work on them to provide you the best content. 

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS