By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

C Search and Replace Function

P: n/a
Hi there, i've been searching for a C String search and replace
function. I need to find all occurrences of " " in a char* array, and
replace them with another char, I know how to do this in managed
environments, but i'm still learning C, does anyone have a quicky
function handy? or point me in the right direction on how to
acccomplish this? your help is greatly appreciated. Also a note, this
is running on a embedded device, so i can't use external libraries, i
will need to build, or find a function that i can put into my existing
code.

Thanks
Warren
Aug 11 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Aug 11, 12:41 pm, Warren Moxley <war...@easy-ebiz.comwrote:
Hi there, i've been searching for a C String search and replace
function. I need to find all occurrences of " " in a char* array, and
replace them with another char, I know how to do this in managed
environments, but i'm still learning C, does anyone have a quicky
function handy? or point me in the right direction on how to
acccomplish this? your help is greatly appreciated. Also a note, this
is running on a embedded device, so i can't use external libraries, i
will need to build, or find a function that i can put into my existing
code.
If you just want to replace all instances of a single character with a
different character as you indicate, all you need to do is scan each
character of the string, check the value of the character and replace
it with the new character if necessary. The process is very straight-
forward, below is a sample function that does this:

void replace_char (char *s, char find, char replace) {
while (*s != 0) {
if (*s == find)
*s = replace;
s++;
}
}

If you need to replace substrings of more than one character the logic
is a bit more complicated. If you need to perform replacement that
can cause the resulting string to be larger than the original string
you will also need a separate buffer or the ability to resize the
original string.

Robert Gamble

Aug 11 '07 #2

P: n/a
On Sat, 11 Aug 2007 17:16:25 -0000, Robert Gamble
<rg*******@gmail.comwrote:
>On Aug 11, 12:41 pm, Warren Moxley <war...@easy-ebiz.comwrote:
>Hi there, i've been searching for a C String search and replace
function. I need to find all occurrences of " " in a char* array, and
replace them with another char, I know how to do this in managed
environments, but i'm still learning C, does anyone have a quicky
function handy? or point me in the right direction on how to
acccomplish this? your help is greatly appreciated. Also a note, this
is running on a embedded device, so i can't use external libraries, i
will need to build, or find a function that i can put into my existing
code.

If you just want to replace all instances of a single character with a
different character as you indicate, all you need to do is scan each
character of the string, check the value of the character and replace
it with the new character if necessary. The process is very straight-
forward, below is a sample function that does this:

void replace_char (char *s, char find, char replace) {
while (*s != 0) {
if (*s == find)
*s = replace;
s++;
}
}

If you need to replace substrings of more than one character the logic
is a bit more complicated. If you need to perform replacement that
can cause the resulting string to be larger than the original string
you will also need a separate buffer or the ability to resize the
original string.

Robert Gamble
Awesome thanks so much for your help, still trying to wrap my head
around c and strings and pointers etc, i'll get it eventually just
might take me a bit. I was getting confused thinking i had to do more
than that, still tying to grasp how c handles char arrays etc.

Cheers !!
Warren
Aug 11 '07 #3

P: n/a
On Aug 11, 1:38 pm, Warren Moxley <war...@easy-ebiz.comwrote:
On Sat, 11 Aug 2007 17:16:25 -0000, Robert Gamble

<rgambl...@gmail.comwrote:
On Aug 11, 12:41 pm, Warren Moxley <war...@easy-ebiz.comwrote:
Hi there, i've been searching for a C String search and replace
function. I need to find all occurrences of " " in a char* array, and
replace them with another char, I know how to do this in managed
environments, but i'm still learning C, does anyone have a quicky
function handy? or point me in the right direction on how to
acccomplish this? your help is greatly appreciated. Also a note, this
is running on a embedded device, so i can't use external libraries, i
will need to build, or find a function that i can put into my existing
code.
If you just want to replace all instances of a single character with a
different character as you indicate, all you need to do is scan each
character of the string, check the value of the character and replace
it with the new character if necessary. The process is very straight-
forward, below is a sample function that does this:
void replace_char (char *s, char find, char replace) {
while (*s != 0) {
if (*s == find)
*s = replace;
s++;
}
}
If you need to replace substrings of more than one character the logic
is a bit more complicated. If you need to perform replacement that
can cause the resulting string to be larger than the original string
you will also need a separate buffer or the ability to resize the
original string.
Robert Gamble

Awesome thanks so much for your help, still trying to wrap my head
around c and strings and pointers etc, i'll get it eventually just
might take me a bit. I was getting confused thinking i had to do more
than that, still tying to grasp how c handles char arrays etc.
I would suggest picking up a good C book ("The C Programming Language
2nd Edition" by K&R is quite good if you aren't new to programming)
and reading the C FAQ <http://www.c-faq.com/>; sections 4, 6, and 8
should help you get a better handle on strings and pointers.

Robert Gamble

Aug 11 '07 #4

P: n/a
On Sat, 11 Aug 2007 18:04:08 -0000, Robert Gamble
<rg*******@gmail.comwrote:
>On Aug 11, 1:38 pm, Warren Moxley <war...@easy-ebiz.comwrote:
>On Sat, 11 Aug 2007 17:16:25 -0000, Robert Gamble

<rgambl...@gmail.comwrote:
>On Aug 11, 12:41 pm, Warren Moxley <war...@easy-ebiz.comwrote:
Hi there, i've been searching for a C String search and replace
function. I need to find all occurrences of " " in a char* array, and
replace them with another char, I know how to do this in managed
environments, but i'm still learning C, does anyone have a quicky
function handy? or point me in the right direction on how to
acccomplish this? your help is greatly appreciated. Also a note, this
is running on a embedded device, so i can't use external libraries, i
will need to build, or find a function that i can put into my existing
code.
>If you just want to replace all instances of a single character with a
different character as you indicate, all you need to do is scan each
character of the string, check the value of the character and replace
it with the new character if necessary. The process is very straight-
forward, below is a sample function that does this:
>void replace_char (char *s, char find, char replace) {
while (*s != 0) {
if (*s == find)
*s = replace;
s++;
}
}
>If you need to replace substrings of more than one character the logic
is a bit more complicated. If you need to perform replacement that
can cause the resulting string to be larger than the original string
you will also need a separate buffer or the ability to resize the
original string.
>Robert Gamble

Awesome thanks so much for your help, still trying to wrap my head
around c and strings and pointers etc, i'll get it eventually just
might take me a bit. I was getting confused thinking i had to do more
than that, still tying to grasp how c handles char arrays etc.

I would suggest picking up a good C book ("The C Programming Language
2nd Edition" by K&R is quite good if you aren't new to programming)
and reading the C FAQ <http://www.c-faq.com/>; sections 4, 6, and 8
should help you get a better handle on strings and pointers.

Robert Gamble
Thank you for your help robert, no im not new to programming, just
with C, mainly a .net, coldfusion developer, also ASM just never had
the need to learn C til now hehe.

Warren Moxley
Aug 11 '07 #5

P: n/a
Warren Moxley wrote:
>
Hi there, i've been searching for a C String search and replace
function. I need to find all occurrences of " " in a char* array,
and replace them with another char, I know how to do this in
managed environments, but i'm still learning C, does anyone have
a quicky function handy? or point me in the right direction on
how to acccomplish this? your help is greatly appreciated. Also
a note, this is running on a embedded device, so i can't use
external libraries, i will need to build, or find a function that
i can put into my existing code.
Take a look at id2id-20, available at:

<http://cbfalconer.home.att.net/download/>

under GPL, and written in portable standard C. Its code is quite
compact, and suited to embedding. The library addition by the
system is not compact, but allows for lots of things not of
interest to you. Id2id operates on identifiers, not characters.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Aug 11 '07 #6

P: n/a
Robert Gamble <rg*******@gmail.comwrites:
[...]
void replace_char (char *s, char find, char replace) {
while (*s != 0) {
if (*s == find)
*s = replace;
s++;
}
}
A minor style point: I would have written

while (*s != '\0')

to emphasize that we're looking for a null character, not just any old
zero value. It's exactly equivalent, but I find '\0' clearer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 11 '07 #7

P: n/a

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
Robert Gamble <rg*******@gmail.comwrites:
[...]
>void replace_char (char *s, char find, char replace) {
while (*s != 0) {
if (*s == find)
*s = replace;
s++;
}
}

A minor style point: I would have written

while (*s != '\0')

to emphasize that we're looking for a null character, not just any old
zero value. It's exactly equivalent, but I find '\0' clearer.
I guess everyone has their own favorite style.
I'd have used:

while(*s)

Less typing. :-)

-Mike

Aug 12 '07 #8

P: n/a
Warren Moxley <wa****@easy-ebiz.comwrites:
Hi there, i've been searching for a C String search and replace
function. I need to find all occurrences of " " in a char* array, and
replace them with another char, I know how to do this in managed
environments, but i'm still learning C, does anyone have a quicky
function handy? or point me in the right direction on how to
acccomplish this? your help is greatly appreciated. Also a note, this
is running on a embedded device, so i can't use external libraries, i
will need to build, or find a function that i can put into my existing
code.

Thanks
Warren
for(;*s;*s++)
if(*s==oldchar)
*s=newchar;

or maybe as a one liner (just for a laugh and untested ....)

while(*s==oldchar?*s++=newchar:*s++);

Aug 12 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.