210k views
5 votes
C++ String access operations

Given string userString, change the second character of userString to 'G'.
Ex: If the input is:
cheetah
then the output is:
cGeetah
Note: Assume the length of string userString is greater than or equal to 2

1 Answer

0 votes

#include <iostream>

#include <string>

using namespace std;

int main() {

string userString;

cout << "Enter a string: ";

cin >> userString;

userString[1] = 'G';

cout << "Modified string: " << userString << endl;

return 0;

}

the user is prompted to enter a string, which is stored in the userString variable. The second character of the string can be accessed and modified using the square bracket syntax, for example userString[1] = 'G'. The modified string is then output.

User Quango
by
7.8k points