473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

changing individual elements of a char variable

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

Dec 2 '06 #1
7 2127
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.
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';
}

Dec 2 '06 #2

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

Dec 2 '06 #3
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
Dec 2 '06 #4
owolablo <ow********@yahoo.comwrote:
>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
Dec 2 '06 #5
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.
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)
Dec 2 '06 #6
"owolablo" <ow********@yahoo.comwrote:
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>

--
To send me email, put "sheltie" in the subject.
Dec 3 '06 #7

"Rolf Magnus" <ra******@t-online.dewrote in message
news:ek*************@news.t-online.com...
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.

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
Dec 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Daniel Lidström | last post by:
Hello! I want to work with individual bytes of integers. I know that ints are 32-bit and will always be. Sometimes I want to work with the entire 32-bits, and other times I want to modify just...
15
by: Jeannie | last post by:
Hello group! I'm in Europe, traveling with my laptop, and I don't any compilers other than Borland C++ 5.5. available. I also don't have any manuals or help files available. Sadly, more...
8
by: whiteboy | last post by:
My assignment is to duplicate the strtok function. My problem is i get "access violation" errors when I try to turn a single char in a char*, as I should. My question is how do I get around this? I...
3
by: KK | last post by:
Hello all, I have several classes binded by one common interface - say 'sum' interface which calculates the sum of all the class elements of type 'int'. class Alphabet { int _a; int _b; int...
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
4
by: vaiism | last post by:
I am trying to write a string compare function using pointers and dynamic arrays, and am having trouble comparing the individual elements of the array contained with the struct. The code below...
60
by: aboxylica | last post by:
my file is of the form 01 "\t" 10.19 "\t" 0.00 "\t" 10.65 02 "\t" 11.19 "\t" 10.12 "\t" 99.99 and...
0
by: rawatgaurav81 | last post by:
I had this strange problem in handling XMLDocuments in asp.net.Though the problem occurs while working on the main application.I will try to explain it with shorter code. I had a form with 2...
2
by: ...vagrahb | last post by:
I am having accessing individual rows from a multidimensional array pass to a function as reference CODE: function Declaration int Part_Buffer(char (*buffer),int Low, int High)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.