0

How to edit a string

I want to change the 2nd character of a string to another character.how do I do that? example: "aabbccdd" to "aZbbccdd"

16th Nov 2016, 2:35 AM
Isaac Pace
Isaac Pace - avatar
5 Answers
+ 4
There are two ways to do this: 1. my_string = "aZbbccdd" 2. my_string[1] = 'Z' C++ treats strings as areays of characters, which means that you can change characters like you change elements in an array.
16th Nov 2016, 2:54 AM
Keto Z
Keto Z - avatar
0
thanks, I think that's right. do you know why I get an error when I run this code though? It says "too many initializers for char[9] #include <iostream> using namespace std; bool game = true; string printBoard(char boardState[]) { string b; b = " | | \n--+--+--\n | | \n--+--+--\n | | "; b[2] = boardState [0]; return b; } int main() { char a[9] = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}; while (game==true ) { string c; c = printBoard (a); cout<<c; return 0; } }
16th Nov 2016, 2:10 PM
Isaac Pace
Isaac Pace - avatar
0
Never mind I figured out the error
16th Nov 2016, 2:16 PM
Isaac Pace
Isaac Pace - avatar
0
chars must be in single quotes
16th Nov 2016, 2:16 PM
Isaac Pace
Isaac Pace - avatar
0
thanks
16th Nov 2016, 2:17 PM
Isaac Pace
Isaac Pace - avatar