473,397 Members | 1,960 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,397 software developers and data experts.

string search and replace

Hi all,

Following is my attempt to write a string search and replace
function.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*-------------------------------------------------------------------------------------------
| Function name : strrep
|
| Arguments : src,find,rep
|
| src string should be *big* enough to hold the result
|
| Return Value : Number of replacements made |
--------------------------------------------------------------------------------------------*/
int strrep(char *src,const char *find,const char *rep)
{
char *remaining; /* pointer to the left over part
*/
int count = 0; /* no. of replacements
*/
size_t find_len = strlen(find); /* length of find string
*/
size_t rep_len = strlen(rep); /* length of replace string */

if(find_len == 0) return 0; /* nothing to find */

/*Initially allocate memory to store whole of src */
if( (remaining = malloc(strlen(src) + 1)) == NULL) /*remember to
free this*/
{
printf("Sorry out of memory !");
return 0;
}
while( (src = strstr(src,find)) != NULL ) /* get the matching
position */
{
count++; /*Count the num of
replacements*/
strcpy(remaining,src + find_len); /*store left over string
*/
strcpy(src,rep); /*make the
replacement */
src += rep_len; /*move to end of
replacement */
strcpy(src,remaining); /*copy back the left
over part */
}

free(remaining);
return count;
}
I know that this code is inefficient (it uses all the string
library routines),
I wanted to know how to improve this code. Can anyone help me ?
I want to know how you would implement the same function
I know that there are some Regex libraries to do this effectively.
But why is such a common function not there in standard library ?
Thanks for your time,
Yugi

Oct 11 '06 #1
5 5449

int main(void) wrote:
Hi all,

Following is my attempt to write a string search and replace
function.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*-------------------------------------------------------------------------------------------
| Function name : strrep
|
| Arguments : src,find,rep
|
| src string should be *big* enough to hold the result
|
| Return Value : Number of replacements made |
--------------------------------------------------------------------------------------------*/
int strrep(char *src,const char *find,const char *rep)
{
char *remaining; /* pointer to the left over part
*/
int count = 0; /* no. of replacements
*/
size_t find_len = strlen(find); /* length of find string
*/
size_t rep_len = strlen(rep); /* length of replace string */

if(find_len == 0) return 0; /* nothing to find */

/*Initially allocate memory to store whole of src */
if( (remaining = malloc(strlen(src) + 1)) == NULL) /*remember to
free this*/
{
printf("Sorry out of memory !");
return 0;
}
while( (src = strstr(src,find)) != NULL ) /* get the matching
position */
{
count++; /*Count the num of
replacements*/
strcpy(remaining,src + find_len); /*store left over string
*/
strcpy(src,rep); /*make the
replacement */
src += rep_len; /*move to end of
replacement */
strcpy(src,remaining); /*copy back the left
over part */
}

free(remaining);
return count;
}
I know that this code is inefficient (it uses all the string
library routines),
I wanted to know how to improve this code. Can anyone help me ?
I want to know how you would implement the same function
I know that there are some Regex libraries to do this effectively.
But why is such a common function not there in standard library ?
Thanks for your time,
Yugi

Sorry for that bad layout. This would be good.

Hi all,
Following is my attempt to write a string search and replace
function.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*---------------------------------------------------------------------------
| Function name : strrep
| Arguments : src,find,rep
| src string should be *big* enough to hold the result
| Return Value : Number of replacements made
----------------------------------------------------------------------------*/
int strrep(char *src,const char *find,const char *rep)
{
char *remaining; /*pointer to the left over part */
int count = 0; /* no.of replacements*/
size_t find_len = strlen(find);/*length of find string*/
size_t rep_len = strlen(rep);/*length of replace string*/

if(find_len == 0) return 0; /* nothing to find */

/*Initially allocate memory to store whole of src */
if( (remaining = malloc(strlen(src) + 1)) == NULL)
{
printf("Sorry out of memory !");
return 0;
}
/* get the matching position*/
while( (src = strstr(src,find)) != NULL )
{
count++; /*Count the num of replacements*/
strcpy(remaining,src + find_len); /*store left over string */
strcpy(src,rep); /*make the replacement*/
src += rep_len; /*move to end of replacement*/
strcpy(src,remaining); /*copy back the left over part*/
}

free(remaining);
return count;
}
I know that this code is inefficient (it uses all the string
library routines),
I wanted to know how to improve this code. Can anyone help me ?
I want to know how you would implement the same function
I know that there are some Regex libraries to do this effectively.
But why is such a common function not there in standard library ?
Thanks for your time,
Yugi

Oct 11 '06 #2

int main(void) wrote:
Hi all,

Following is my attempt to write a string search and replace
function.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*-------------------------------------------------------------------------------------------
| Function name : strrep
|
| Arguments : src,find,rep
|
| src string should be *big* enough to hold the result
|
| Return Value : Number of replacements made |
--------------------------------------------------------------------------------------------*/
int strrep(char *src,const char *find,const char *rep)
{
char *remaining; /* pointer to the left over part
*/
int count = 0; /* no. of replacements
*/
size_t find_len = strlen(find); /* length of find string
*/
size_t rep_len = strlen(rep); /* length of replace string */

if(find_len == 0) return 0; /* nothing to find */

/*Initially allocate memory to store whole of src */
if( (remaining = malloc(strlen(src) + 1)) == NULL) /*remember to
free this*/
{
printf("Sorry out of memory !");
return 0;
}
while( (src = strstr(src,find)) != NULL ) /* get the matching
position */
{
count++; /*Count the num of
replacements*/
strcpy(remaining,src + find_len); /*store left over string
*/
strcpy(src,rep); /*make the
replacement */
src += rep_len; /*move to end of
replacement */
strcpy(src,remaining); /*copy back the left
over part */
}

free(remaining);
return count;
}
I know that this code is inefficient (it uses all the string
library routines),
I wanted to know how to improve this code. Can anyone help me ?
I want to know how you would implement the same function
I know that there are some Regex libraries to do this effectively.
But why is such a common function not there in standard library ?
Thanks for your time,
Yugi

Sorry for that bad layout. This would be good.

Hi all,
Following is my attempt to write a string search and replace
function.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*---------------------------------------------------------------------------
| Function name : strrep
| Arguments : src,find,rep
| src string should be *big* enough to hold the result
| Return Value : Number of replacements made
----------------------------------------------------------------------------*/
int strrep(char *src,const char *find,const char *rep)
{
char *remaining; /*pointer to the left over part */
int count = 0; /* no.of replacements*/
size_t find_len = strlen(find);/*length of find string*/
size_t rep_len = strlen(rep);/*length of replace string*/

if(find_len == 0) return 0; /* nothing to find */

/*Initially allocate memory to store whole of src */
if( (remaining = malloc(strlen(src) + 1)) == NULL)
{
printf("Sorry out of memory !");
return 0;
}
/* get the matching position*/
while( (src = strstr(src,find)) != NULL )
{
count++; /*Count the num of replacements*/
strcpy(remaining,src + find_len); /*store left over string */
strcpy(src,rep); /*make the replacement*/
src += rep_len; /*move to end of replacement*/
strcpy(src,remaining); /*copy back the left over part*/
}

free(remaining);
return count;
}
I know that this code is inefficient (it uses all the string
library routines),
I wanted to know how to improve this code. Can anyone help me ?
I want to know how you would implement the same function
I know that there are some Regex libraries to do this effectively.
But why is such a common function not there in standard library ?
Thanks for your time,
Yugi

Oct 11 '06 #3
int main(void) wrote:

Sending the same message three times does not endear you to us.
Sending the same message thrre times does not endear you to us.
Sending the same message three times does not endeer you to us.
But why is such a common function not there in standard library ?
Likely:

Because search-and-replace may need to expand the target string,
and there isn't an obvious single best way to do this.

(And because there wasn't one commonly implemented when the first
standard was produced.)

--
Chris "Essen -8 and counting" Dollin
"A facility for quotation covers the absence of original thought." /Gaudy Night/

Oct 11 '06 #4

Chris Dollin wrote:
int main(void) wrote:

Sending the same message three times does not endear you to us.
Sending the same message thrre times does not endear you to us.
Sending the same message three times does not endeer you to us.
My apologies. I sent the second time because layout of the code
after
posting got srambled somehow.
I dont know why it got posted the third time.
I'm using google interface.
>
But why is such a common function not there in standard library ?

Likely:

Because search-and-replace may need to expand the target string,
and there isn't an obvious single best way to do this.

(And because there wasn't one commonly implemented when the first
standard was produced.)
Thanks for that point.
Thanks for your time,
Yugi.

Oct 11 '06 #5
"int main(void)" wrote:
Chris Dollin wrote:
>int main(void) wrote:
.... snip about string search and replace ...
>>
>>But why is such a common function not there in standard library ?

Likely:

Because search-and-replace may need to expand the target string,
and there isn't an obvious single best way to do this.

(And because there wasn't one commonly implemented when the first
standard was produced.)

Thanks for that point.
And because the function is rarely needed. It may be useful in
replacing strings during a file copy operation, which is a
different matter. It might be instructive to examine how
identifiers are replaced in id2id, for which see id2id-20.zip at:

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

--
Some informative links:
<news:news.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
Oct 11 '06 #6

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...
4
by: beliavsky | last post by:
The code for text in open("file.txt","r"): print text.replace("foo","bar") replaces 'foo' with 'bar' in a file, but how do I avoid changing text inside single or double quotes? For making...
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...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
7
by: DarthBob88 | last post by:
I have to go through a file and replace any occurrences of a given string with the desired string, like replacing "bug" with "feature". This is made more complicated by the fact that I have to do...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
5
by: buu | last post by:
I have an function that replaces some string from a huge text that I run very often... So, I wanted to speed it up... I was using String and StringBuilder. But, I was wandering should same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.