Change String Characters
To change the value of a specific character in a string, refer to the index number, and use single quotes:
Example:1
string n = “Hello”;
n[0] = ‘K’;
cout << n;
Output
Kello instead of Hello
Example:2
string n = “Hello”;
n[1] = ‘a’;
cout << n;
Output
Hallo instead of Hello
//To change/modify the elements of a string
#include<iostream>
#include<string>
using namespace std;
int main()
{
string n="Hello";
cout<<"string is "<<n<<endl;
n[0]='K';
cout<<"string is "<<n<<endl;
n="Hello";
n[1]='a';
cout<<"string is "<<n<<endl;
return(0);
}
Output:
string is Hello
string is Kello
string is Hallo




