0

可能重复:
strstr 是否有反向 fn()

我编写了这个 main 和函数,它获取两个字符串并检查第二个字符串是否存在于第一个字符串中,然后返回最右边的索引出现位置。如果没有找到返回-1

这是我写的代码:

#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;
}

我认为我在这一行有问题:

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

因为我尝试将第二个字符串包含在第一个字符串中,但是虽然t[j]等于 null 它不执行内部 if 语句。

  • 我知道还有很多其他方法可以编写这个程序。但我知道这个也应该工作,我正在努力让它工作。
4

2 回答 2

0

您正在处理索引,这很令人困惑。索引只用于一个目的,不要随意设置和重置。我建议稍微重写你的函数并清理使用的索引

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;
}

当然,您可以替换内部循环strncmp

现在回答你的问题;-)。您的strindex作品按预期进行。正如@wildplasser 已经指出的那样,fgets将最后的换行符存储\n在缓冲区中。由于要搜索的字符串中除了末尾没有换行符,因此如果要搜索的字符串位于中间而不是末尾,则永远不会得到匹配项。

当您从a[]and中删除换行符时b[],您会看到 strindex 有效。另一种方法可能是在命令行上给出字符串,而不是使用 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;
}
于 2012-12-09T14:07:47.557 回答
0

好的朋友。这是有效的解决方案:-):感谢 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;
}
于 2012-12-09T17:59:12.273 回答