【问题标题】:How to find out if a word is inside of a line?如何找出一个单词是否在一行内?
【发布时间】:2021-02-06 18:06:12
【问题描述】:

我正在尝试从“C 编程”一书中的练习中创建一个函数。正确的函数应该指示一行是否包含某个单词,如果是 - 返回它在该行中的第一个字符位置(单词的)。

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

int substring (char a[], char b[]);

int main ()
{
    char line1[15], line2[15];
    printf("Print first one\n");
    
    int i = 0;
    char character;
    do 
    {
        character = getchar ();
        line1[i] = character;
        i++;
    }
    while (character != '\n');
    line1[i-1] = '\0';

    printf ("Print the second one\n");
    scanf("%s", line2);

    printf("%s,  %s\n", line1, line2); \\ for checking lines

    int index;

    index = substring (line1, line2);
    printf("The result is: %i\n", index);

}

int substring (char a[], char b[])\*function to determine if a line contains a word, if yes then return its polition, else return -3*\
{
    int len1 = strlen(a), len2 = strlen(b);

    int current1 = 0, current2 = 0;
    bool found = false;
    int result;
    while( current1 < len1 )
    {
        if (a[current1] == b[current2])
        {
            if(!found)
            {
                result = current1+1;
            }
            else 
                found = true;
                    
            while ((a[current1] == b[current2]) && (a[current1] != '\0') && (b[current2] != '\0'))
            {
                
                current1++;
                    
                if(current2+1 == len2)
                    return result;
                    
                current2++;
            }
            current1 = result;
            }
        else
        {
            current2 = 0;
            found = false;
            current1++;
        }
    }
    return -3;
}

问题出在第二个函数(子字符串)中,因为当我尝试在“Here is your word”行中搜索“word”时,功能正常工作,但是当我尝试在一行中搜索“word”时“这是你的 wwwwword”,函数返回 -3(这表明是否出现问题)。

【问题讨论】:

  • getchar 返回 int,而不是 char。你需要检查它是否返回EOF,所以你不能使用较小的整数类型。
  • 听起来你有一个错误。如果不仔细查看您的代码,我猜您匹配w,然后将wo 不匹配,然后您认为您正在寻找w,但实际上您应该是寻找o。也就是说,您没有正确重置搜索。 IOW,而不是current2 = 0,在某些情况下,您可能需要检查您是否真的想要current2 = 1
  • 请注意,如果您正在寻找像abcabcd 这样的字符串并且输入了abcabcabcd,这会变得更加复杂,在这种情况下,您需要将该指针重置为 4 而不是 0。跨度>

标签: c search substring c-strings function-definition


【解决方案1】:

对于初学者来说,有一个标准的 C 字符串函数strstr,它允许确定一个字符串是否包含在另一个字符串中。

如果你需要自己编写这样的函数,那么你的函数实现看起来太复杂了。

例如 if-else 语句

    if (a[current1] == b[current2])
    {
        if(!found)
        {
            result = current1+1;
        }
        else 
            found = true;
        // ...

至少有两个原因没有多大意义。第一个是变量result,它可以从while循环中的函数返回,不包含找到的单词的确切索引。第二个是 found 在函数内永远不能等于 true。所以if语句中的条件

if( !found )

总是评估为真。

另外你忘了在while循环之后的if语句的复合语句中重置变量current2

除此之外,函数参数应该有限定符const,因为传递的字符串不会在函数中更改。函数返回类型为size_t

该函数可以如下面的演示程序所示。

#include <stdbool.h>
#include <string.h>

size_t substring( const char *s1, const char *s2 )
{
    size_t n1 = strlen( s1 );
    size_t n2 = strlen( s2 );

    size_t i = 0;
    bool found = false;

    if (!( n1 < n2 ))
    {
        for ( size_t n = n1 - n2; !found && i <= n; i += !found )
        {
            size_t j = 0;
            while (s2[j] != '\0' && s2[j] == s1[i + j]) ++j;

            found = s2[j] == '\0';
        }
    }

    return found ? i : -1;
}

int main( void )
{
    const char *word = "word";
    const char *s1 = "Here is your word";
    const char *s2 = "Here is your wwwwword";

    printf( "%zu\n", substring( s1, word ) );
    printf( "%zu\n", substring( s2, word ) );
}

程序输出是

13
17

如果在源字符串中找不到单词,则该函数返回类型为 size_t 的值,该值等于 -1

注意变量字符应该声明为int而不是char。否则,如果 char 类型与 unsigned char 类型一样,则与 EOF 的比较通常会产生意外结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2013-01-07
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多