#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    31
    Rep Power
    12

    c : String compare?


    How do I find a (common) sub-string that occurs in 2 strings that is entered by user . Then print out the (common) sub-string. Program terminates when no (common) sub-string is found.

    Eg user input : competition petrol
    program returns : pet

    user input : conclude icon
    program returns : con

    user input : eat ate
    program returns : at

    user input : cat dog
    program terminates

    Thank you.
  2. #2
  3. No Profile Picture
    Dinesh_P_V
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    India
    Posts
    259
    Rep Power
    0
    Just a quick thought. I have not compiled it.

    char str1[100],str2[100];
    int len1,len2,i,j;
    printf("\nEnter : ");
    scanf("%s%s",str1,str2);
    len1=strlen(str1);
    len2=strlen(str2);
    for(i=0;i<len1;i++)
    for(j=0;j<len2;j++)
    {
    if(str1[i]==str2[j])
    {
    for(;i<len1;i++)
    for(;j<len2;j++)
    {
    if(str1[i]==str2[j])
    {
    printf("%c",str2[j]);
    i++;
    }
    else return;
    }
    return;
    }
    }

    HTH
    -Murugesan
    Dinesh_P_V
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    31
    Rep Power
    12

    Re: c : String compare?


    Originally posted by poppers
    How do I find a (common) sub-string that occurs in 2 strings that is entered by user . Then print out the (common) sub-string. Program terminates when no (common) sub-string is found.

    Eg user input : competition petrol
    program returns : pet

    user input : conclude icon
    program returns : con

    user input : eat ate
    program returns : at

    user input : cat dog
    program terminates

    Thank you.
    I missed out an important point, that is the 2 strings are entered when running the executable. Assume com.exe is the executable.

    input at command prompt : com eat ate
    output : at

    Question 1 : How does the executable take in 2 arguments?

    Question 2: I am totally clueless on how to pick out the sub-string occuring in the 2 strings. Pls help?
    Last edited by poppers; September 17th, 2003 at 01:26 PM.
  6. #4
  7. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Just join argv[1] and argv[2].
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    31
    Rep Power
    12
    Originally posted by murugesan
    Just a quick thought. I have not compiled it.

    char str1[100],str2[100];
    int len1,len2,i,j;
    printf("\nEnter : ");
    scanf("%s%s",str1,str2);
    len1=strlen(str1);
    len2=strlen(str2);
    for(i=0;i<len1;i++)
    for(j=0;j<len2;j++)
    {
    if(str1[i]==str2[j])
    {
    for(;i<len1;i++)
    for(;j<len2;j++)
    {
    if(str1[i]==str2[j])
    {
    printf("%c",str2[j]);
    i++;
    }
    else return;
    }
    return;
    }
    }

    HTH
    -Murugesan
    Thank you for offering the solution.

    It complied fine but doesnt work.

    I appreciate your effort, thank you.
  10. #6
  11. No Profile Picture
    Dinesh_P_V
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    India
    Posts
    259
    Rep Power
    0
    I don't get your point.
    You have mentioned to
    >>print out the (common) sub-string.
    for competition and petrol
    the first common sub string is "o" next one is "pet" next is "et" next is "t" ... and it goes.
    Could you please explain

    -Murugesan
  12. #7
  13. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    If you input the two strings, then compare each corresponding character from one string with the other, you can find any matching sequence of two or more characters.

    If you then 'slide' one of the strings along by one character and repeat the search comparing corresponding characters, you'll find any further matches. Slide along again and search again. Repeat until the string has 'slid past' (there's nothing left to compare).

    Then repeat, sliding in the other direction (or slide the other string).

    I hope that makes sense. If not, try it on paper.

    The first search you're comparing:
    string1[0] with string2[0]
    string1[1] with string2[1]
    string1[2] with string2[2] and so on.

    The next search you're comparing:
    string1[0] with string2[1]
    string1[1] with string2[2]
    string1[2] with string2[3] and so on.

    The next search you're comparing:
    string1[0] with string2[2]
    string1[1] with string2[3]
    string1[2] with string2[4] and so on.

    The first time you find a matching sequence of 2 or more characters, continue until the match fails or you get to the end of the string, then stop.

    I have a working version, so the idea is sound (but I'm sure you'd rather code it yourself) :-)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    31
    Rep Power
    12
    thank you.

    it sure make sense!

IMN logo majestic logo threadwatch logo seochat tools logo