String manipulation in C++ using string library
In this tutorial, we will see how to perform string manipulation in C++ and learn about various functions of the string library.
A string is a sequence of characters. We use the String library in C++ to create and implement string objects. It has the implementation of a character array.
In C++ we can declare strings in two ways:
- Create a fixed-size character array.
- Create a variable size string using the string as the data type.
#include<iostream> #include<string>// for string class using namespace std; int main() { //character array with predefined size = 40 char char_array[40] = "CodeSpeedy as a character array"; // variable sized string string str("CodeSpeedy as a string"); cout << char_array << endl; cout << str << endl; return 0; }
String manipulation functions in C++
Some of the commonly used string manipulations functions of the string library in C++
- length()
This method finds the length of a given string. It returns an integer value as the length. - size()
This method also finds the length of a given string and returns an integer value.
- concatenation methods
1. “+” operator: We can concatenate two strings and assign them to yet another string without affecting the value of the initials strings using this operator.
2. “+=” operator: We can concatenate two strings and assign them to either of the two strings itself by using this operator.
3. append(): We can append the values of a string to a given string at the end using this function. - clear(): The function of the string library clears all the contents of a given string.
- empty(): This function checks if the given is empty or not and returns 0 or 1 as output.
- swap(): This swaps the contents of two strings seamlessly.
- substr(start_index, no_of_characters*): This returns the substring of a string starting from start index up to the given number of characters. If the optional second argument is not given, it returns a substring up to the end of the string.
- at(index): This function fetches and returns a character at a given index in the string.
- replace(start_index, no_of_characters, replace_by): This replaces the given number of characters starting from the start index by the replce_by argument.
- find (substring): This function returns the index of the first occurrence of the substring and returns -1 if the substring is not found.
The following code illustrates an example of each of the above-discussed functions.
C++ Implementation: Working with string
#include <iostream> #include <string> using namespace std; int main() { string my_string = "CodeSpeedy"; //find length of the string using length() and size() functions int len = my_string.length(); cout<<len<<endl; int size = my_string.size(); cout<<size<<endl; //concatinating two strings using '+' operator string str1 = "Hello"; string str2 = "world"; string new_string = str1 + str2; cout << new_string << endl; //concatinating two strings using '+=' operator string s1 = "CodeSpeedy"; string s2 = "tutorials"; cout<< s1<< endl; cout<< s2<< endl; s1+=s2; cout<< s1<< endl; cout<< s2<< endl; //concatinating two strings using append() function string st1 = "CodeSpeedy"; string st2 = "tutorials"; cout<< st1<< endl; cout<< st2<< endl; st1.append(st2); cout<< st1<< endl; cout<< st2<< endl; //clear a string using Clear() //and check if it is epty using empty() functions string str = " Hello World!"; cout<< str<< endl; (str.empty()==1)?cout<<"string empty" :cout<<"string not empty"<< endl; str.clear(); (str.empty()==1)?cout<<"string empty" :cout<<"string not empty"<< endl; //swapping two strings using swap() function string str3 = "Hello"; string str4 = "world"; cout<< "str3 = "<<str3<<endl; cout<< "str4 = "<< str4<< endl; str3.swap(str4); cout<< "after swapping :"<< "str3 = "<<str3<<endl; cout<< "str4 = "<< str4<< endl; //getting substring from a string using substr(). cout<<str.substr(2,5)<<endl; // substring from index 2-5 cout<<str.substr(4)<<endl;//substring starting //from index 4 to the end of the string //getting a charcter at a particular index using at() cout<<str.at(4)<<endl; //replacing a subtring at a particular index //in a given string with another using replace() s = "String manipulation in C++" cout<< s << endl; s.replace(4,3,"x"); cout<< s << endl; //find a given substring in a string using find() function t = "thankyou "; int index = t.find(th); cout<< index << endl; return 0; }
Further reading: Find the last index of a character in a string in C++,
Mirror of a string in C++ (Reversal of string) and Removing leading and trailing spaces from a string in C++
Leave a Reply