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
|