C++ Operations on String
In this tutorial, you'll learn Operations on string in C++. You'll learn to declare them, initialize them and use them for various input/output operations.
Strings
A string is a Variable that stores a sequence of letters or other characters. C++ has in its definition a way to represent sequence of characters as an object of class. This class is called std:: string.
String class stores the characters as a sequence of bytes with a functionality of allowing access to single byte character.
Just like the other data types, to create a string we first declare it, then we can store a value in it.
Operations on Strings
Input Functions
S.no. | Input Functions | Descriptions |
---|---|---|
1. | getline() | This function is used "to store a stream of characters" as entered by the user in the object memory. |
2. | push_back() | This function is used to "input" a character at the "end" of the string. |
3. | pop_back() | Introduced from C++11 (for strings), This function is used to "delete the last character" from the string. |
Example 1: Working of Input Functions
// Program to show working of getline(), push_back() and pop_back().
#include <iostream>
#include <string>
using namespace std;
int main(){
string myStr; // Declaring string
cout << "Enter a string: "; // Taking string input using getline()
getline(cin, myStr);
cout << "Displaying initial string is: ";
cout << myStr << endl;
myStr.push_back('s'); // using push_back() to insert a character at the end
cout << "Displaying a string after push_back operation: ";
cout << myStr << endl;
myStr.pop_back(); // using pop_back() to delete a character from end
cout << "Displaying a string after pop_back operation: ";
cout << myStr << endl;
return 0;
}
Output
Enter a string: Program Displaying initial string is: Program Displaying a string after push_back operation: Programs Displaying a string after pop_back operation: Program
Capacity functions
S.no. | Capacity Functions | Descriptions |
---|---|---|
4. | capacity() | This function "returns the capacity" allocated to the string, which can be equal to or equal to or more than the size of the string. Additional space is allocated so that when the new characters are added to the string, the operations can be done efficiently. |
5. | resize() | This function "changes the size of string", the size can be increased or decreased. |
6. | length() | This function "finds the length of the string". |
7. | shrink_to_fit() | This function "decreases the capacity" of the string and makes it equal to the minimum capacity of the string. This operation "useful to save additional memory" if we are sure that no further addition of characters have to be made. |
Example 2: Working of Capacity Functions
// Program to show working of capacity(), resize() and shrink_to_fit().
#include <iostream>
#include <string>
using namespace std;
int main(){
string myStr = "Program"; // Initializing string
cout << "Displaying initial string is: ";
cout << myStr << endl;
myStr.resize(13); // using resize() to resize the string
cout << "Displaying a string after resize operation: ";
cout << myStr << endl;
cout << "Displaying the capacity of string is: "; // print the capacity of strings
cout << myStr.capacity() << endl;
cout << "Displaying the length of the string: ";
cout << myStr.length() << endl;
myStr.shrink_to_fit(); // using shrink_to_fit() to decrease the string
cout << "Displaying new capacity after shrinking the string: ";
cout << myStr.capacity() << endl;
return 0;
}
Output
Displaying initial string is: Program Displaying a string after resize operation: Program...... Displaying the capacity of string is: 15 Displaying the length of the string: 13 Displaying new capacity after shrinking the string: 15
Iterator Functions
S.no. | Iterator Functions | Descriptions |
---|---|---|
8. | begin() | This function returns an "iterator" to beginning of the string. |
9. | end() | This function returns an "iterator" to end of the string. |
10. | rbegin() | This function returns an "reverse iterator" pointing at the end of the string. |
11. | rend() | This function returns a "reverse iterator" pointing at the beginning of the string. |
Example 3: Working of Iterator Functions
// Program to show working of begin(), end(), rbegin() and rend().
#include <iostream>
#include <string>
using namespace std;
int main(){
string myStr = "Program"; // Initializing string
std::string::iterator it; //Declaring iterator
std::string::reverse_iterator it1; //Declaring reverse iterator
cout << "Displaying the string using forward iterators: ";
for (it = str.begin(); it != str.end(); it++)
cout << *it;
cout<< endl;
//Declaring reverse string
cout << "Displaying the reverse string using reverse iterators: ";
for (it1 = str.rbegin(); it1 != str.rend(); it1++)
cout << *it1;
cout << endl;
return 0;
}
Output
Displaying the string using forward iterators: Program Displaying the reverse string using reverse iterators: margorP
Manipulating Functions
S.no. | Manipulating Functions | Descriptions |
---|---|---|
12. | copy("char array", len, pos) | This function "copies the substring in target character array" mentioned in its arguments. It takes 3 arguments, target char array, length to be copied and starting position in string to start copying. |
13. | swap() | This function swaps one string with other. |
Example 4: Working of Manipulating Functions
// Program to show working of copy() and swap().
#include <iostream>
#include <string>
using namespace std;
int main(){
string myStr1 = "Programming"; // Initializing 1st string
string myStr2 = "Coding"; // Declaring 2nd string
char ch[50];
myStr1.copy(ch,11,0); // using copy() to copy elements into char array copies "Programming"
cout << "Displaying 1st string before swapping: ";
cout << myStr1 << endl;
cout << "Displaying 2nd string before swapping: ";
cout << myStr2 << endl;
// using swap() to swap string content
myStr1.swap(myStr2);
cout << "Displaying 1st string after swapping: ";
cout << myStr1 << endl;
cout << "Displaying 2nd string after swapping: ";
cout << myStr2 << endl;
return 0;
}
Output
Displaying 1st string before swapping: Programming Displaying 2nd string before swapping: Coding Displaying 1st string after swapping: Coding Displaying 2nd string after swapping: Programming
We hope that this tutorial helped you develop better understanding of the concept of String Objects in C++.
Keep Learning : )