search and replace 
Author Message
 search and replace

given a text string "str", i want to search for string "pat" and replace
it with "dup"

so i want to replace all occurances of "pat" with "dup" in "str".

Is there any standard library fuction (or may be a combination of them)
to do so.

I am using gcc on linux2.2

cheers
vivek



Fri, 30 Jul 2004 16:11:56 GMT  
 search and replace


Wed, 18 Jun 1902 08:00:00 GMT  
 search and replace
Since the string to be searched & the one to be replaced are of sam
length....we can do this.

Use strstr() to locate your sub-string "pat".
If strstr() can not find "pat" if will return NULL.
Now you could replace the next 3 chars with..'d','u','p'.

main()
{
char inputString[]="my string having a pat in it";
char lookFor[]="pat";
char * ptr = NULL;
    :
    :
    if ( ptr=strstr(inputString, lookFor)
    {
      *ptr = 'd';        /*Replace next 3 chars with dup...can't use strcpy
here !!!*/
       ptr++;
      *ptr = 'u';
       ptr++;
      *ptr = 'p';
    }
:
:

Quote:
}

-Kasp


Fri, 30 Jul 2004 16:20:28 GMT  
 search and replace

Quote:

> main()

C99 requires `int' to be explicitly written out explicitly as the
function's return type, and it's a good idea even in C90.

Quote:
> {
> char inputString[]="my string having a pat in it";
> char lookFor[]="pat";
> char * ptr = NULL;
>     :
>     :
>     if ( ptr=strstr(inputString, lookFor)

Missing right )

Quote:
>     {
>       *ptr = 'd';        /*Replace next 3 chars with dup...can't use strcpy
> here !!!*/
>        ptr++;
>       *ptr = 'u';
>        ptr++;
>       *ptr = 'p';

To reduce confusion I'd write this some other way:
        ptr[0] = 'd';
        ptr[1] = 'u';
        ptr[2] = 'p';
or
        *ptr++ = 'd';
        *ptr++ = 'u';
        *ptr++ = 'p';
or
        memcpy(ptr, "dup", 3);
Quote:
>     }
> :
> :
> }



Fri, 30 Jul 2004 16:23:29 GMT  
 search and replace

i agree that the code u gave can do the job ...
i am not a very experieced C user, and my rule of thumb is to stick to
library functions as far as possible.

So i wonder why is there no library function for search and replace ...
something which (i feel) is so common in string handling.

cheers
vivek

Quote:

> Since the string to be searched & the one to be replaced are of sam
> length....we can do this.

> Use strstr() to locate your sub-string "pat".
> If strstr() can not find "pat" if will return NULL.
> Now you could replace the next 3 chars with..'d','u','p'.

> main()
> {
> char inputString[]="my string having a pat in it";
> char lookFor[]="pat";
> char * ptr = NULL;
>     :
>     :
>     if ( ptr=strstr(inputString, lookFor)
>     {
>       *ptr = 'd';        /*Replace next 3 chars with dup...can't use strcpy
> here !!!*/
>        ptr++;
>       *ptr = 'u';
>        ptr++;
>       *ptr = 'p';
>     }
> :
> :
> }

> -Kasp



Fri, 30 Jul 2004 17:00:41 GMT  
 search and replace

Quote:

> Since the string to be searched & the one to be replaced are of sam
> length....we can do this.

> Use strstr() to locate your sub-string "pat".
> If strstr() can not find "pat" if will return NULL.
> Now you could replace the next 3 chars with..'d','u','p'.

> main()
> {
> char inputString[]="my string having a pat in it";
> char lookFor[]="pat";
> char * ptr = NULL;
>     :
>     :
>     if ( ptr=strstr(inputString, lookFor)
>     {
>       *ptr = 'd';        /*Replace next 3 chars with dup...can't use strcpy
> here !!!*/
>        ptr++;
>       *ptr = 'u';
>        ptr++;
>       *ptr = 'p';
>     }
> :
> :
> }

> -Kasp

You are right that you can't use strcpy.  However, strncpy works fine,
and is cleaner than your solution.

if (ptr=strstr(inputString, lookFor) {
  strncpy(ptr, replaceString, strlen(replaceString));

Quote:
}

strncpy will not add a terminating null character in this case.

-Peter



Sat, 31 Jul 2004 05:58:04 GMT  
 search and replace


Wed, 18 Jun 1902 08:00:00 GMT  
 search and replace

Quote:

> so i want to replace all occurances of "pat" with "dup" in "str".

Doesn't seem so hard, does it? You don't even need a standard library function
;-)

        david

--
If 91 were prime, it would be a counterexample to your conjecture.
    -- Bruce Wheeler



Sun, 01 Aug 2004 08:49:16 GMT  
 search and replace


Wed, 18 Jun 1902 08:00:00 GMT  
 search and replace


...

Quote:
>You are right that you can't use strcpy.  However, strncpy works fine,
>and is cleaner than your solution.

>if (ptr=strstr(inputString, lookFor) {
>  strncpy(ptr, replaceString, strlen(replaceString));
>}

However memcpy() has simpler semantics and does the same thing
in this context, so

   memcpy(ptr, replaceString, strlen(replaceString));

is better.

Quote:
>strncpy will not add a terminating null character in this case.

When strncpy() doesn't add a terminating null, character it behaves
exactly like memcpy().

--
-----------------------------------------


-----------------------------------------



Wed, 04 Aug 2004 22:43:21 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Search and Replace facility

2. Newbie: Sub-String search and replace routine

3. Newbie: Sub-String search and replace routine

4. Optimization problem search and replace

5. String - search and replace

6. search and replace strings with lists

7. how to do search and replace?

8. regex search and replace

9. How to Search&Replace C Data Types ?

10. search and replace

11. Binary file Search and Replace

12. search and replace in a string

 

 
Powered by phpBB® Forum Software