Question

Possible Duplicate:
Is there a reverse fn() for strstr

I wrote this main and function that get two string and check if the second string exist within the first one, then return the most right index appear place. If wasn't found return -1.

This is the code I wrote:

#include <stdio.h>

int strindex(char[], char[]);

int main()
{
    char a[100];
    char b[100];
    int search;
    printf("Enter two strings, To search the second one in the first one:\n");
    printf("Enter the first string to search in:\n");
    fgets(a,100,stdin);
    printf("Enter the second string to search in the first:\n");
    fgets(b,100,stdin);
    printf("\n\n THE FIRST STRING IS:%s\n\n THE SEARCH STRING IS:%s",a, b);
    printf("\n\n");
    search = strindex(a, b);
    if(search==-1)
        printf("The second String didnt found in the first string\n");
    else printf("The second string appear in the first string at most right at index:%d\n",search);
    return 0;
}


int strindex(char s[], char t[])
{
    int foundIndex = -1;
    int tempFound, startNext;
    int i,j;
    i = j = 0;
    while (s[i]!='\0')
    {
        if(s[i]==t[j])
        {
            startNext = i+1;
            tempFound = i;
            while(s[i]!='\0' && t[j]!='\0' && s[i]==t[j])
                i++,j++;
            if (t[j]=='\0') /**check if it null**/
            {
                printf("found match");
                foundIndex = tempFound;
            }
            i = startNext;
            j = 0;
        }
        else i++;
    }
    return foundIndex;
}

I think I have a problem in this line:

if (t[j]=='\0') /**check if it null**/

cause I tried put two string that the second one contain in the first but although t[j] is equal to null it doesn't perform the inside if statement.

  • I know there are many other ways of writing this program. But I know this one should work too and i am trying to make it work.
Was it helpful?

Solution

You're juggling with indexes, which is quite confusing. Use the indexes for one purpose only and don't set and reset at will. I would suggest to rewrite your function a bit and cleanup the used indexes

int strindex(char s[], char t[])
{
    int foundIndex = -1;
    int i,j;
    for (i = 0; s[i]!='\0'; i++)
    {
        if(s[i]==t[0])
        {
            for(j = 1; s[i + j]!='\0' && t[j]!='\0' && s[i + j]==t[j]; j++)
                ;

            if (t[j]=='\0') /**check if it null**/
            {
                printf("found match");
                foundIndex = i;
            }
        }
    }

    return foundIndex;
}

You can replace the inner loop with strncmp of course.

And now to your question ;-). Your strindex works as intended. As @wildplasser already noted, fgets stores the final newline \n in the buffer. Since there's no newline in the string to be searched except at the end, you never get a match, if the string to search for is in the middle and not at the end.

When you either strip the newline from a[] and b[], you will see, that strindex works. Another approach could be to give the strings on the command line instead of reading with fgets.

int main(int argc, char **argv)
{
    int search;
    printf("\n\n THE FIRST STRING IS:%s\n\n THE SEARCH STRING IS:%s", argv[1], argv[2]);
    printf("\n\n");
    search = strindex(argv[1], argv[2]);
    if(search==-1)
        printf("The second String didnt found in the first string\n");
    else printf("The second string appear in the first string at most right at index:%d\n",search);
    return 0;
}

OTHER TIPS

OK friends. this is the solution that works:-): thanks to Olaf Dietsche

#include <stdio.h>

int strindex(char[], char[]);

int main()
{
    char a[100];
    char b[100];
    int search;
    printf("Enter two strings, To search the second one in the first one:\n");
    printf("Enter the first string to search in:\n");
    fgets(a,100,stdin);
    printf("Enter the second string to search in the first:\n");
    fgets(b,100,stdin);
    printf("\n\n THE FIRST STRING IS:%s\n\n THE SEARCH STRING IS:%s",a, b);
    printf("\n\n");
    search = strindex(a, b);
    if(search==-1)
        printf("The second String didnt found in the first string\n");
    else printf("The second string appear in the first string at most right at index:%d\n",search);
    return 0;
}


int strindex(char s[], char t[])
{
    int foundIndex = -1;
    int tempFound, startNext;
    int i,j;
    i = j = 0;
    while (s[i]!='\n')
    {
        if(s[i]==t[j])
        {
            startNext = i+1;
            tempFound = i;
            while(s[i]!='\n' && t[j]!='\n' && s[i]==t[j])
                i++,j++;
            if (t[j]=='\n')
                foundIndex = tempFound;
            i = startNext;
            j = 0;
        }
        else i++;
    }
    return foundIndex;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top