changing individual elements of a char variable

O

owolablo

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.
Thanks
 
R

Rolf Magnus

owolablo said:
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.

Then I guess you don't mean a char variable, but rather an array of char,
otherwise there is only one single character and nothing to parse. It's
best to not use raw arrays of char, but instead the class std::string. It's
a lot easier to use.

#include <string>

int main()
{
using std::string;
string s("Hello world");
string::size_type index = s.find('e');
s[index] = 'X';
}
 
G

Gavin Deane

owolablo said:
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.

A char variable doesn't have "individual elements", unless perhaps you
could consider the individual bits that make up its representation to
be "individual elements", but I don't think that is your question.

char c = 'c';
A char variable is just that, a single character.

I think your question is, how do I replace all occurrances of a
particular char with a different char in a string. If that is your
question, then:

1. Stop using null-terminated arrays of char to represent strings in
C++. Use std::string. It makes your life (including this sort of
question) so much easier.
2. The standard library provides an algorithm, std::replace, that does
what you need.

Gavin Deane
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 
S

Steve Pope

owolablo said:
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.
Thanks

You might want to look up "fields", but seldom is it really
necessary to use them.

Steve
 
P

Pete Becker

owolablo said:
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.
Thanks

void replace(char *str, char tgt, char repl)
{
char *pos = strchr(str, tgt);
while (pos)
{
*pos = repl;
pos = strchr(pos + 1, tgt);
}
}


--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
D

Daniel T.

owolablo said:
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.

Assuming 's' is a null terminated string, and old_value and new_value
are both chars:

std::replace( s, s + strlen( s ), old_value, new_value );

The above is in the #include header <algorithm>
 
M

Mike Wahler

Rolf Magnus said:
Then I guess you don't mean a char variable, but rather an array of char,
otherwise there is only one single character and nothing to parse. It's
best to not use raw arrays of char, but instead the class std::string.
It's
a lot easier to use.

#include <string>

int main()
{
using std::string;
string s("Hello world");
string::size_type index = s.find('e');

if(index != std::string::npos) // :)
s[index] = 'X';
}

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top