You can do it like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string in;
cout << "Input please\n";
cin >> in;
if(in.size() >= 5)
in[5] = 'A';
cout << in << "\n";
return 0;
}
Or you can use std::getline(), instead of std::cin.
Output:
Input please
samaras
samarAs
However, are you sure this is the bottleneck of your program? You can check this with some profiling tools, like the one I use.
[EDIT]
Since OP is asking about efficiency, I did some testing. However, you can to take into account the time that user takes to type the input, but since I am the same person, we can assume this is constant.
So, I did modified a bit a code from another answer, like this:
std::string str;
cout << "Input please\n";
std::getline(std::cin, str);
if (str.size() >= 5) {
str[5] = '#';
}
std::cout << str << "\n";
Output:
Input please
Samaras
Samar#s
It took me 1.04237 seconds.
And with my code, I got
Input please
Samaras
SamarAs
It took me 0.911217 seconds.
Which actually show that they are pretty close and I would say the difference is due to my typing speed.
I did the timings with std::chrono, like the code I have in my pseudo-site.