On 2006-12-02 20:50, owolablo wrote:
Can anybody please tell me how to change the individual elements of a
char variable. I need to parse through the string, check for a
particular character and change it to something else if it is found.
I suspect you mean a char*/char[], since a normal char is just one
element (a character). One way would be to put it in a string and then
iterate through the string until you find the character you want and
change it.
std::string s(char_string);
std::string::iterator i = s.begin();
for (; i != s.end(); ++i)
{
if (*i == 'x') *i = 'y';
}
where char_string is your char-variable. You can do the same thing
without using string too:
for (char* i = char_string; *i != '\0'; ++i)
{
if (*i == 'x') * i = 'y';
}
but I'd like the first one better. There might exist a STL-algorithm for
doing things like this but I don't know of any.
--
Erik Wikström