#include <string>
#include <iostream>
#include <algorithm>

int main()
{

   using std::string;
   using std::cout;
   using std::endl;
   using std::replace;
   string s("name");


   replace(s.begin(), s.end(), 'n', 'x');
   replace(s.begin(), s.end(), 'a','x');

   cout << s << endl;
system("pause");
   return 0;
}

Here is my code. this code output= xxame. my desired output is that their is a cin to input a word then replace 1st two letters. anyone could make it for me or any suggestions.

If you need to replace the first two letters, you can access them directly:

string s = "name";
cout<<s[0]<<endl;
cout<<s[1]<<endl;

thus

s[0] = 'x';
s[1] = 'x';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.