Operations on Strings in C++
July 15, 2022
Strings in C++ are generally implemented as the objects of the string class. They can be implemented as an array of characters too (it is indeed faster in terms of implementation), but the built-in functions that come with the string class make objects the obvious choice for implementing strings.
If you aren’t aware of the different types of implementation of strings in C++, check this post out.
Similar to arrays, various operations can be performed on strings as well. Let’s learn more about them today ✨
🚨 Disclaimer: To use any built-in functions in the string class, you need to include the string class in your header. It is done using #include<string> 🚨
1. Input a string
There are multiple functions which you can use to input a string. We’ll talk about one of the most widely used function: getline()
Since cin treats every whitespace as a terminating character, this function is used to input a string. If you don't know what a terminating character is, read this post and come back 🏋️
The getline() function itself can be used in 2 ways:
1. If string is implemented as a character array
Before discussing this further, you must be wondering what delimiter is right? Can you try to guess it? Let me break it down in simple words 🧱
Delimiter here basically is a character or a sequence of characters that indicate the beginning or end of a specific string.
Let’s take an example. Say I have to input a string BUT I want to stop the input if ‘$’ is identified in the input. So if I write ‘Byte$ Avenue’ in the input, it should only take Byte as the input since $ is coming after "Byte". The $ here acts as a delimiter and identifies where a string is beginning/ ending.
If you want to input a string using your own delimiter, you can use the getline() function like this 👇
P.S. When you're using this way to input a string, you need to define the maximum length till where you want the input in the parameters of the function.
2. If string is implemented as an object of the string class
Taking an input in this manner is kind of similar. Only thing that changes is the operation (cin) goes inside the parentheses of the function and the max length part goes away.
If you want to input a string using this way, you can use the getline() function like this 👇
Here name is the string object that needs to be mentioned and '$' is the delimiter. If you don't put the delimiter in the parameters, the default parameter is taken as '\n'.
2. Finding length of a string
The length of a string is found out using the length() function. There are no parameters for this function. It can be used like this 👇
This will print the length of the string, i.e. 32.
3. Resizing the length of a string
This is done using the resize() function. There are no parameters for this function. It can be used like this 👇
This will print the first 4 elements of the string, i.e. Byte.
4. Swapping strings
The swap() function swaps one string with another. The syntax is str1.swap(str2) where str1 and str2 are strings that need to be swapped with each other. It can be used like this 👇
This will swap Byte and Avenue and the value of str1 will become Byte where the value of str2 will become Avenue.
5. Concatenating strings
What is concatenating? It simply means joining 2 strings end to end. For example, the concatenation of "Byte" and "Avenue" is "ByteAvenue".
We learned previously that the two ways to represent string are:
1. String class
2. Character array
1. To find concatenation for strings declared/initialized from a string class, we use the append() function
2. Similarly, you can concatenate two strings using the '+' operator
3. For character arrays, we use the strcat() function
For all of these the output is "ByteAvenue"
6. Finding substring of a string
As the name suggests, a substring is a part of a string. For an example “Byte ” and “Avenue” are substrings of the string “Byte Avenue”. Even “By” is a substring of the string “Byte Avenue”.
To finding a substring of string, we can use the substr() function.
This function takes in two parameters substr(pos, len):
1. pos- This is the index where the substring starts from.
2. len- This is the length of substring taken into account after the pos index.
You can use the substr() function to find a substring like this 👇
s2 will be "yte"
There are many other in-built functions in the string class that can be used to perform various other operations on strings. DM me a ✨ on Instagram if you want a PDF containing all the in-built functions in the string class.
Hope this is helpful and gives you an insight on what different operations can be performed on strings using the in-built functions in the string class in C++ 😌 You can send me a mail at email@example.com or DM me on Instagram if you have any questions 💪
Until next time 👋
Want to learn more?
Our first course, Introduction to Data Structures in C++ worth ₹499/month is available at a price of your choice for the first month access.
Offer available for a limited time