C Search and Replace Function

W

Warren Moxley

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
 
R

Robert Gamble

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
 
W

Warren Moxley

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
 
R

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
 
W

Warren Moxley

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
 
C

CBFalconer

Warren said:
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.
 
K

Keith Thompson

Robert Gamble said:
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.
 
M

Mike Wahler

Keith Thompson said:
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
 
R

Richard

Warren Moxley said:
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++);
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top