String Search & Replace 
Author Message
 String Search & Replace

i am trying to implement an algo that it can
search a string for a char occurence and replace
it with a string.

eg:
  string to be searched: "abcdef"
  char to search for: "c"
  string to replace it with: "12"
  output of the functin should be "ab12def"

any ideas?

TIA
Killer Angel

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sat, 22 Mar 2003 03:00:00 GMT  
 String Search & Replace
I am trying to implement a function that receives as input 2 strings
and a char and it outputs string1 with every occurence of char replaced
with string2

eg:
  string1: "abcdefc"
  char: "c"
  string2: "12"
  output: "ab12def12"

TIA

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 22 Mar 2003 03:00:00 GMT  
 String Search & Replace

Quote:

> i am trying to implement an algo that it can
> search a string for a char occurence and replace
> it with a string.

> eg:
>   string to be searched: "abcdef"
>   char to search for: "c"
>   string to replace it with: "12"
>   output of the functin should be "ab12def"

> any ideas?

Yes. Use strstr() and sprintf(), then DYODH.

Richard



Sat, 22 Mar 2003 03:00:00 GMT  
 String Search & Replace

Quote:

> i am trying to implement an algo that it can
> search a string for a char occurence and replace
> it with a string.

> eg:
>   string to be searched: "abcdef"
>   char to search for: "c"
>   string to replace it with: "12"
>   output of the functin should be "ab12def"

> any ideas?

Here's an idea. Why not write a C function to do this?

It's not difficult, and we'll help if you get stuck. But you have to
make the first move.

Here's a hint, though, to get you started: strstr(), strlen(), pointer
arithmetic, sprintf(), more or less in that order.

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R Answers: http://users.powernet.co.uk/eton/kandr2/index.html



Sat, 22 Mar 2003 03:00:00 GMT  
 String Search & Replace

Quote:

> i am trying to implement an algo that it can
> search a string for a char occurence and replace
> it with a string.

Use strchr or strcspn to look for your character. Use strcpy, strcat,
strncpy and strncat to contract your new string.

Be careful you don't overflow your buffers.

Bi555ll,



Sat, 22 Mar 2003 03:00:00 GMT  
 String Search & Replace

Quote:

>> i am trying to implement an algo that it can
>> search a string for a char occurence and replace
>> it with a string.

>> eg:
>>   string to be searched: "abcdef"
>>   char to search for: "c"
>>   string to replace it with: "12"
>>   output of the functin should be "ab12def"

>> any ideas?

>Yes. Use strstr() and sprintf(), then DYODH.

sprintf is overkill and not enough for the job.  He'll need memmove to
shrink/expand the existing string and memcpy to insert the replacement
substring.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sat, 22 Mar 2003 03:00:00 GMT  
 String Search & Replace

Quote:

>i am trying to implement an algo that it can
>search a string for a char occurence and replace
>it with a string.

>eg:
>  string to be searched: "abcdef"
>  char to search for: "c"
>  string to replace it with: "12"
>  output of the functin should be "ab12def"

>any ideas?

I'm in a homework assignment mood, so I'll do it for you.  But:

1. I'll replace a substring by another substring.

2. I won't offer any comments.  You'll have to figure out how it works
   by yourself.

    #include <string.h>

    char *repstr(char *s, char *s1, char *s2)
    {
        char *p = strstr(s, s1);
        size_t l1 = strlen(s1), l2 = strlen(s2);

        if (p == NULL) return NULL;
        memmove(p + l2, p + l1, strlen(p + l1) + 1);
        memcpy(p, s2, l2);
        return s;
    }

Just as in the case of strcat(), the function assumes that the result fits
into the allocated space.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sat, 22 Mar 2003 03:00:00 GMT  
 String Search & Replace


Quote:

> >i am trying to implement an algo that it can
> >search a string for a char occurence and replace
> >it with a string.

> >eg:
> >  string to be searched: "abcdef"
> >  char to search for: "c"
> >  string to replace it with: "12"
> >  output of the functin should be "ab12def"

> >any ideas?

> I'm in a homework assignment mood, so I'll do it for you.  But:

> 1. I'll replace a substring by another substring.

> 2. I won't offer any comments.  You'll have to figure out how it works
>    by yourself.

>     #include <string.h>

>     char *repstr(char *s, char *s1, char *s2)
>     {
>    char *p = strstr(s, s1);
>    size_t l1 = strlen(s1), l2 = strlen(s2);

>    if (p == NULL) return NULL;
>    memmove(p + l2, p + l1, strlen(p + l1) + 1);
>    memcpy(p, s2, l2);
>    return s;
>     }

> Just as in the case of strcat(), the function assumes that the result
fits
> into the allocated space.

> Dan
> --
> Dan Pop
> CERN, IT Division

> Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland

Thanx very much Dan. I got it working

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 23 Mar 2003 03:00:00 GMT  
 String Search & Replace


Quote:
>     #include <string.h>

>     char *repstr(char *s, char *s1, char *s2)
>     {
>    char *p = strstr(s, s1);
>    size_t l1 = strlen(s1), l2 = strlen(s2);

Tangential question:  to what extent are you allowed to precede a
variable declaration with an expression?  Above, you call strstr before
declaring l1.  In C++, you can put declarations anywhere; in C they
have to be at the top of a function.  How stringent is that? E.g., I
thought something like

void function(void) {
  int a=0;
  a++;
  int b;
   .....

is wrong; int b should precede a++.  Your lines are correct (since you
know what you're doing and the code compiled); what is the actual
rule?  (For ANSI C.)

TIA,

sjfromm

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 23 Mar 2003 03:00:00 GMT  
 String Search & Replace

Quote:



>>     #include <string.h>

>>     char *repstr(char *s, char *s1, char *s2)
>>     {
>>        char *p = strstr(s, s1);
>>        size_t l1 = strlen(s1), l2 = strlen(s2);
> Tangential question:  to what extent are you allowed to precede a
> variable declaration with an expression?  Above, you call strstr before
> declaring l1.  In C++, you can put declarations anywhere; in C they
> have to be at the top of a function.

not as of C99.

Quote:
> How stringent is that? E.g., I
> thought something like
> void function(void) {
>   int a=0;
>   a++;
>   int b;
>    .....
> is wrong; int b should precede a++.  Your lines are correct (since you
> know what you're doing and the code compiled); what is the actual
> rule?  (For ANSI C.)

all declarations in C89 must appear at the top of the scope.  each
declaration may have an initialiser, though.

--
 /"\                                                 m i k e   b u r r e l l

  X        AGAINST HTML MAIL,



Sun, 23 Mar 2003 03:00:00 GMT  
 String Search & Replace

Quote:



>>     #include <string.h>

>>     char *repstr(char *s, char *s1, char *s2)
>>     {
>>        char *p = strstr(s, s1);
>>        size_t l1 = strlen(s1), l2 = strlen(s2);

>Tangential question:  to what extent are you allowed to precede a
>variable declaration with an expression?  Above, you call strstr before
>declaring l1.  In C++, you can put declarations anywhere; in C they
>have to be at the top of a function.  How stringent is that? E.g., I
>thought something like

>void function(void) {
>  int a=0;
>  a++;
>  int b;
>   .....

>is wrong; int b should precede a++.

Right, C89 does not allow this.  C99 does, but this is irrelevant to this
discussion, because the code I wrote was supposed to be usable with
current compilers.

Quote:
>Your lines are correct (since you
>know what you're doing and the code compiled); what is the actual
>rule?  (For ANSI C.)

C89:

    All the expressions in an initializer for an object that has static
    storage duration or in an initializer list for an object that has
    aggregate or union type shall be constant expressions.
...
    The initializer for a scalar shall be a single expression,
    optionally enclosed in braces.  The initial value of the object is
    that of the expression; the same type constraints and conversions as
    for simple assignment apply.

So, I am allowed to use an arbitrary expression as initialiser for an
automatic scalar, but not for one with static storage duration.

My code above is equivalent to:

    char *repstr(char *s, char *s1, char *s2)
    {
        char *p;
        size_t l1, l2;

        p = strstr(s, s1);
        l1 = strlen(s1);
        l2 = strlen(s2);

I prefer to initialise my variables at the point of their declaration,
if possible.  This generates code which is more compact and more readable.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sun, 23 Mar 2003 03:00:00 GMT  
 String Search & Replace

Quote:

> >     char *repstr(char *s, char *s1, char *s2)
> >     {
> >       char *p = strstr(s, s1);
> >       size_t l1 = strlen(s1), l2 = strlen(s2);

> Tangential question:  to what extent are you allowed to precede a
> variable declaration with an expression?  Above, you call strstr before
> declaring l1.

But strstr() is just functioning to initialize p, which
is declared on the same line.  I.e. the "char *p = ..." line
is a declaration too, so we're OK.  

Allin Cottrell.



Sun, 23 Mar 2003 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. String : Search & Replace

2. string search & replace

3. How to Search&Replace C Data Types ?

4. Search & Replace

5. search & replace in set of files

6. ?? Search & Replace with CFindReplaceDialog and CEditView

7. Newbie: Sub-String search and replace routine

8. Newbie: Sub-String search and replace routine

9. String - search and replace

10. search and replace strings with lists

11. search and replace in a string

12. String Search And Replace

 

 
Powered by phpBB® Forum Software