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"
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.
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;
}
}
0
Never mind I figured out the error
0
chars must be in single quotes
0
thanks