473,372 Members | 967 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,372 software developers and data experts.

C Search and Replace Function

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
8 22088
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
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
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
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: gyromagnetic | last post by:
Hi, I have written a function that searches a text string for various words. The text is searched using a boolean 'and' or a boolean 'or' of the input list of search terms. Since I need to use...
1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
4
by: Ken Fine | last post by:
I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string, return the highlighted search term along with the...
10
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x";...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
5
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h>...
1
by: coolami4u | last post by:
I need a program that simulates the search-and-replace operation in a text editor. The program is to have only three function calls in main. The first function prompts the user to type a string of...
3
by: peterhall | last post by:
In VBA an Access module has a find method - works perfectly to find a string inside a module. i'm working in A97 (legacy) systems (large ones) and want to write code that searches all modules so that...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
5
by: silmana | last post by:
Hi i have this script that i want to use as php or html but i cant find the problem, could anyone solve the problem, i dont know why i cannot use it in php or html file // OBS! Några saker måste...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...

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.