How to Add A Character To A String in C++
Hey, so you probably came here wondering the same question: How to Add a character to a string in C++.
The code is simple and is really easy to execute so understanding it wouldn’t be any problem for you at all.
So let’s go ahead and take a look at it.
Add A Character To A String In C++
There are 2 ways of solving this:
- The simple method by adding a character after the string
- Using the String Append Function.
I’ll be explaining both so you can perform whichever you find easier.
The Simple Method:
Step 1: Including Header files
Along with the common header file ‘iostream’ you’ll also have to include ‘string’ since you’ll be performing operations on string.
I have used ‘using namespace std;’ to prevent writing ‘std::’ before every cin and cout. Consider it a shortcut.
#include<iostream> #include<string> using namespace std;
Step 2 is to accept the string from the user in the main function and along with that the character you want to add after the string
I’ll be storing the string in variable ‘str’ of type string and the character to be added in variable ‘char’
string str; cout<<"Enter a string: "; cin>>str; //Storing the string char addtext; cout<<"Enter the character you would like to add: "; cin>>addtext; //Storing the character
Step 3 is where the main operation occurs. We’re adding the character to the string using the following line and then printing it.
str = str + addtext;
If you have followed me up till here, your code should look like this:
#include<iostream> #include<string> using namespace std; int main() { string str; cout<<"Enter a string: "; cin>>str; //Storing the string char addtext; cout<<"Enter the character you would like to add: "; cin>>addtext; //Storing the character str = str + addtext; cout<<str; }
This is how your output window should look like:
Enter a string: Hell Enter the character you would like to add: o Hello
Using String Append:
Here we’re using a predefined function of String called append. It’s used to join a character or a string to end of the first string.
Syntax:
string_name.append(number_of_times_element_should_be_added , ‘character’)
We’re keeping the number of times an element should be added as 1 since we want to print the character only once after the string. If you want to print it multiple times after the string, feel free to play around with the number
If you choose to use this, Step 1 and 2 would be identical to the simple method.
However, for step 3 we would write:
str.append(1,addtext);
Therefore your source code should look like:
#include<iostream> #include<string> using namespace std; int main() { string str; cout<<"Enter a string: "; cin>>str; char addtext; cout<<"Enter the character you would like to add: "; cin>>addtext; str.append(1,addtext); cout<<str; }
Your Output Screen would look like:
Enter a string: CodeSpeed Enter the character you would like to add: y CodeSpeedy
And that is all. I hope you understood the logic and were able to execute it by yourself. If you have any doubts regarding this, feel free to ask it in the comment section. Thank You.
Leave a Reply