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 8 21921
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
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
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
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
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
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"
"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
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++); This discussion thread is closed Replies have been disabled for this discussion. Similar topics
2 posts
views
Thread by gyromagnetic |
last post: by
|
1 post
views
Thread by Les Juby |
last post: by
|
4 posts
views
Thread by Ken Fine |
last post: by
|
10 posts
views
Thread by pembed2003 |
last post: by
|
5 posts
views
Thread by pembed2003 |
last post: by
|
5 posts
views
Thread by int main(void) |
last post: by
| | | | | | | | | | | | | | |