Find first occurrence of string and replace

Discussion in 'C Programming' started by Registered User, Oct 16, 2006.

  1. Hi experts,
    I'm trying to write a program to solve the following exercise:

    Accept three strings from the user. Find the first occurrence of the
    first string in the third string. If it is present replace the second
    string in its place.
    Note: First and Second string might not be of the same length.
    E.g.: First string: "cat"
    Second string: "camel"
    Third string: "concatenate"
    Output: "concamelenate"

    I've made a complete working program. The following is the main() part
    of my program.
    int main()
    {
    char s1[80], s2[80], s3[80];
    int pos;

    /*Code for accepting 3 strings from user*/
    /*and storing in s1, s2 and s3*/

    pos=find(s1, s3); /*find() returns the position at which */
    /*s1 occurs in s3, -1 otherwise*/

    delete(pos, s3, strlen(s1)); /*Deletes strlen(s1) characters from s3
    starting at pos*/
    /*Does not delete if pos<0*/

    insert(pos, s3, s2);/*Inserts s2 in s3 at location specified by pos*/
    /*Does nothing if pos<0*/


    puts(s3);
    }

    So, the three operations I've used for finding and replacing are:
    (1)find
    (2)delete and
    (3)insert
    My question is: Is there a better way of doing this?
    Registered User, Oct 16, 2006
    #1
    1. Advertising

  2. On Sun, 15 Oct 2006, Registered User wrote:
    >
    > Hi experts,
    > I'm trying to write a program to solve the following exercise:
    >
    > Accept three strings from the user. Find the first occurrence of the
    > first string in the third string. If it is present replace the second
    > string in its place.
    > Note: First and Second string might not be of the same length.
    > E.g.: First string: "cat"
    > Second string: "camel"
    > Third string: "concatenate"
    > Output: "concamelenate"
    >
    > I've made a complete working program. The following is the main() part
    > of my program.
    > int main()
    > {
    > char s1[80], s2[80], s3[80];
    > int pos;
    >
    > /*Code for accepting 3 strings from user*/
    > /*and storing in s1, s2 and s3*/
    >
    > pos=find(s1, s3); /*find() returns the position at which */
    > /*s1 occurs in s3, -1 otherwise*/
    >
    > delete(pos, s3, strlen(s1)); /*Deletes strlen(s1) characters from s3
    > starting at pos*/
    > /*Does not delete if pos<0*/
    >
    > insert(pos, s3, s2);/*Inserts s2 in s3 at location specified by pos*/
    > /*Does nothing if pos<0*/


    What if strlen(s3)+strlen(s2) >= 80? Does 'insert' behave differently
    then, or have you got a buffer overflow?
    http://en.wikipedia.org/wiki/Buffer_overflow

    Also, you could speed up some cases by deleting or inserting only
    as many characters as you need to, and writing over the others. For
    example, to get from "concatenate" to "concamelenate", you'd delete
    zero characters, insert two characters ("concatxxenate"), and then
    overwrite all five characters ("concamelenate"). To get from
    "concatenate" to "condogenate", you wouldn't insert or delete any
    characters; just write "dog" at the right point in "concatenate"
    and you're done.

    HTH,
    -Arthur
    Arthur J. O'Dwyer, Oct 16, 2006
    #2
    1. Advertising

  3. On Sun, 15 Oct 2006, Arthur J. O'Dwyer wrote:

    >
    > On Sun, 15 Oct 2006, Registered User wrote:
    >>
    >> Hi experts,
    >> I'm trying to write a program to solve the following exercise:
    >>
    >> Accept three strings from the user. Find the first occurrence of the
    >> first string in the third string. If it is present replace the second
    >> string in its place.
    >> Note: First and Second string might not be of the same length.
    >> E.g.: First string: "cat"
    >> Second string: "camel"
    >> Third string: "concatenate"
    >> Output: "concamelenate"
    >>
    >> I've made a complete working program. The following is the main() part
    >> of my program.
    >> int main()
    >> {
    >> char s1[80], s2[80], s3[80];
    >> int pos;
    >>
    >> /*Code for accepting 3 strings from user*/
    >> /*and storing in s1, s2 and s3*/
    >>
    >> pos=find(s1, s3); /*find() returns the position at which */
    >> /*s1 occurs in s3, -1 otherwise*/
    >>
    >> delete(pos, s3, strlen(s1)); /*Deletes strlen(s1) characters from s3
    >> starting at pos*/
    >> /*Does not delete if pos<0*/
    >>
    >> insert(pos, s3, s2);/*Inserts s2 in s3 at location specified by pos*/
    >> /*Does nothing if pos<0*/

    >
    > What if strlen(s3)+strlen(s2) >= 80? Does 'insert' behave differently
    > then, or have you got a buffer overflow?
    > http://en.wikipedia.org/wiki/Buffer_overflow
    >
    > Also, you could speed up some cases by deleting or inserting only
    > as many characters as you need to, and writing over the others. For
    > example, to get from "concatenate" to "concamelenate", you'd delete
    > zero characters, insert two characters ("concatxxenate"), and then
    > overwrite all five characters ("concamelenate"). To get from
    > "concatenate" to "condogenate", you wouldn't insert or delete any
    > characters; just write "dog" at the right point in "concatenate"
    > and you're done.


    One could also merge the the three operations together by
    using strstr(), memmove() and memcpy() in a single function:

    #include <string.h>

    char *found = strstr(s3, s1);
    size_t len = strlen(s2);

    if (found) {
    /* Exercise: insert bounds checking code here */
    memmove(found + len,
    found,
    strlen(s3) - strlen(s1) - (found - s3));
    memcpy(found, s2, len);
    }

    Tak-Shing
    Tak-Shing Chan, Oct 16, 2006
    #3
  4. On Mon, 16 Oct 2006, Tak-Shing Chan wrote:

    > On Sun, 15 Oct 2006, Arthur J. O'Dwyer wrote:
    >
    >>
    >> On Sun, 15 Oct 2006, Registered User wrote:
    >>>
    >>> Hi experts,
    >>> I'm trying to write a program to solve the following exercise:
    >>>
    >>> Accept three strings from the user. Find the first occurrence of the
    >>> first string in the third string. If it is present replace the second
    >>> string in its place.
    >>> Note: First and Second string might not be of the same length.
    >>> E.g.: First string: "cat"
    >>> Second string: "camel"
    >>> Third string: "concatenate"
    >>> Output: "concamelenate"
    >>>
    >>> I've made a complete working program. The following is the main() part
    >>> of my program.
    >>> int main()
    >>> {
    >>> char s1[80], s2[80], s3[80];
    >>> int pos;
    >>>
    >>> /*Code for accepting 3 strings from user*/
    >>> /*and storing in s1, s2 and s3*/
    >>>
    >>> pos=find(s1, s3); /*find() returns the position at which */
    >>> /*s1 occurs in s3, -1 otherwise*/
    >>>
    >>> delete(pos, s3, strlen(s1)); /*Deletes strlen(s1) characters from s3
    >>> starting at pos*/
    >>> /*Does not delete if pos<0*/
    >>>
    >>> insert(pos, s3, s2);/*Inserts s2 in s3 at location specified by pos*/
    >>> /*Does nothing if pos<0*/

    >>
    >> What if strlen(s3)+strlen(s2) >= 80? Does 'insert' behave differently
    >> then, or have you got a buffer overflow?
    >> http://en.wikipedia.org/wiki/Buffer_overflow
    >>
    >> Also, you could speed up some cases by deleting or inserting only
    >> as many characters as you need to, and writing over the others. For
    >> example, to get from "concatenate" to "concamelenate", you'd delete
    >> zero characters, insert two characters ("concatxxenate"), and then
    >> overwrite all five characters ("concamelenate"). To get from
    >> "concatenate" to "condogenate", you wouldn't insert or delete any
    >> characters; just write "dog" at the right point in "concatenate"
    >> and you're done.

    >
    > One could also merge the the three operations together by
    > using strstr(), memmove() and memcpy() in a single function:
    >
    > #include <string.h>
    >
    > char *found = strstr(s3, s1);
    > size_t len = strlen(s2);
    >
    > if (found) {
    > /* Exercise: insert bounds checking code here */
    > memmove(found + len,
    > found,
    > strlen(s3) - strlen(s1) - (found - s3));
    > memcpy(found, s2, len);
    > }


    Sorry, the memmove() line above is wrong. It should be:

    memmove(found + len,
    found + strlen(s1),
    strlen(s3) - strlen(s1) - (found - s3));

    Tak-Shing
    Tak-Shing Chan, Oct 16, 2006
    #4
  5. On Mon, 16 Oct 2006, Tak-Shing Chan wrote:

    > On Mon, 16 Oct 2006, Tak-Shing Chan wrote:
    >
    >> On Sun, 15 Oct 2006, Arthur J. O'Dwyer wrote:
    >>
    >>>
    >>> On Sun, 15 Oct 2006, Registered User wrote:
    >>>>
    >>>> Hi experts,
    >>>> I'm trying to write a program to solve the following exercise:
    >>>>
    >>>> Accept three strings from the user. Find the first occurrence of the
    >>>> first string in the third string. If it is present replace the second
    >>>> string in its place.
    >>>> Note: First and Second string might not be of the same length.
    >>>> E.g.: First string: "cat"
    >>>> Second string: "camel"
    >>>> Third string: "concatenate"
    >>>> Output: "concamelenate"
    >>>>
    >>>> I've made a complete working program. The following is the main() part
    >>>> of my program.
    >>>> int main()
    >>>> {
    >>>> char s1[80], s2[80], s3[80];
    >>>> int pos;
    >>>>
    >>>> /*Code for accepting 3 strings from user*/
    >>>> /*and storing in s1, s2 and s3*/
    >>>>
    >>>> pos=find(s1, s3); /*find() returns the position at which */
    >>>> /*s1 occurs in s3, -1 otherwise*/
    >>>>
    >>>> delete(pos, s3, strlen(s1)); /*Deletes strlen(s1) characters from s3
    >>>> starting at pos*/
    >>>> /*Does not delete if pos<0*/
    >>>>
    >>>> insert(pos, s3, s2);/*Inserts s2 in s3 at location specified by pos*/
    >>>> /*Does nothing if pos<0*/
    >>>
    >>> What if strlen(s3)+strlen(s2) >= 80? Does 'insert' behave differently
    >>> then, or have you got a buffer overflow?
    >>> http://en.wikipedia.org/wiki/Buffer_overflow
    >>>
    >>> Also, you could speed up some cases by deleting or inserting only
    >>> as many characters as you need to, and writing over the others. For
    >>> example, to get from "concatenate" to "concamelenate", you'd delete
    >>> zero characters, insert two characters ("concatxxenate"), and then
    >>> overwrite all five characters ("concamelenate"). To get from
    >>> "concatenate" to "condogenate", you wouldn't insert or delete any
    >>> characters; just write "dog" at the right point in "concatenate"
    >>> and you're done.

    >>
    >> One could also merge the the three operations together by
    >> using strstr(), memmove() and memcpy() in a single function:
    >>
    >> #include <string.h>
    >>
    >> char *found = strstr(s3, s1);
    >> size_t len = strlen(s2);
    >>
    >> if (found) {
    >> /* Exercise: insert bounds checking code here */
    >> memmove(found + len,
    >> found,
    >> strlen(s3) - strlen(s1) - (found - s3));
    >> memcpy(found, s2, len);
    >> }

    >
    > Sorry, the memmove() line above is wrong. It should be:
    >
    > memmove(found + len,
    > found + strlen(s1),
    > strlen(s3) - strlen(s1) - (found - s3));


    Oops, I forgot the terminating '\0':

    memmove(found + len,
    found + strlen(s1),
    strlen(s3) - strlen(s1) - (found - s3) + 1);

    Tak-Shing
    Tak-Shing Chan, Oct 16, 2006
    #5
  6. In comp.lang.c Tak-Shing Chan <> wrote:

    > > Sorry, the memmove() line above is wrong. It should be:


    > Oops, I forgot the terminating '\0':


    Perhaps posting on Mondays is a bad idea :)

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Oct 16, 2006
    #6
  7. Registered User

    Ed Collins Guest

    > Is there a better way of doing this?

    Your method looks fine. In the real world, time is money. An argument can
    be made that whatever method works for you, especially if it's simple to
    code, understand, and maintain, can be considered "better." Are there more
    elegant solutions? Possibly. Are the "better?" Not necessarily.

    I don't code in C but here's one way to do it PowerBASIC. (This probably
    won't help you, since C almost surely doesn't have its own EXTRACTS$
    function, but I couldn't resist.)


    first$ = "cat" : second$ = "dog"
    third$ = "Actually, I believe cat is man's best friend!"

    a$ = EXTRACT$(third$, first$)

    IF a$ <> third$ THEN
    a$ = a$ + second$ + RIGHT$(third$, LEN(third$) - LEN(a$) - LEN(first$))
    END IF


    In the above example, the variable a$ now is equal to "Actually, I believe
    dog is man's best friend!"

    Ed Collins



    "Registered User" <> wrote in message
    news:...

    > Hi experts,
    > I'm trying to write a program to solve the following exercise:


    > rest snipped...
    Ed Collins, Oct 18, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jorn W Janneck
    Replies:
    3
    Views:
    2,854
    Jorn W Janneck
    Sep 5, 2003
  2. Jack
    Replies:
    8
    Views:
    137
  3. andyo
    Replies:
    1
    Views:
    759
    Vincent Fourmond
    Feb 27, 2007
  4. Replies:
    12
    Views:
    438
    Ben Morrow
    Jul 9, 2008
  5. David Karr
    Replies:
    4
    Views:
    188
    Ben Bacarisse
    Sep 16, 2013
Loading...

Share This Page