【问题标题】:Finding a substring, using pointers使用指针查找子字符串
【发布时间】:2018-12-11 16:59:54
【问题描述】:

正如标题中提到的,我想检查一个子字符串是否找到了另一个字符串。

#include <stdio.h>
#include <stdlib.h>

int isIncluded(char *text, char* pattern);

int main()
{

    char text[30];
    char pattern[30]; int result;

    printf(" Please introduce your text \n");
    scanf("%s", &text);

    printf(" Please introduce the pattern you are looking for \n");
    scanf("%s", &pattern);



    result = isIncluded( &text, &pattern);

    if ( result == 1)
    {

        printf(" Your pattern has been found in your text \n " ) ;
    }
    if ( result == 0)
    {

        printf(" no substring found \n " ) ;
    }

}


int isIncluded(char *text, char* pattern)
{

    int ct = 0;
    int numberofcharacters = 0;

    while ( *pattern != '\0')
    {
        pattern++;
        numberofcharacters++;

    }

    while ( *text != '\0' && pattern != '\0')
    {
        if ( *pattern == *text)
        {
            pattern++;
            ct++;
            text++;

        }
        else
        {
           text++;
        }

    }

    if ( ct == numberofcharacters ) 
    {
        return(1);
    }
    else
    {
            return(0);
    }


}

思路是将文本变量的第一个字符与模式变量进行比较,举个例子:

假设我们在文本变量中有“TEXT”,在模式中有“EX”:

我开始比较T和E,在这种情况下,不匹配。

我指向 E 并再次比较,有一个匹配。

由于匹配,我在pattern中指向X并在文本中做同样的事情,然后我再做一次测试。

第2次匹配,因此pattern变量中的字符数将与ct变量相同,只有匹配时才计算。

因此返回应该等于1。

代码总是返回零。我不明白为什么?

【问题讨论】:

  • C 标准库附带strstr()&lt;string.h&gt; 中的原型),它可以满足您的需求:)
  • 您的patternisIncluded 的第一个循环之后就被丢弃了...
  • test 是一个 char 数组和 数组名 本身的地址,因此 scanf("%s", &amp;text); --> scanf("%s", text); 同样适用于 pattern
  • 我认为您应该在调试器中单步执行您的函数,并检查它是否真的按照您的意愿/您认为的那样。
  • 现在我正在发现有趣的调试工具,尝试逐步修复。当我更正我的程序时,我会发布它。

标签: c substring


【解决方案1】:
    #include <stdio.h>
#include <stdlib.h>

int isIncluded(char *text, char* pattern);

int main()
{

char text[30];
char pattern[30]; int result;

printf(" Please introduce your text \n");
scanf("%s", text);

printf(" Please introduce the pattern you are looking for \n");
scanf("%s", pattern);



result = isIncluded( text, pattern);
if ( result == 1)
{

printf(" Your pattern has been found in your text \n " ) ;
}
if ( result == 0)
{

printf(" no substring found \n " ) ;
}



}


int isIncluded(char *text, char* pattern)
{

char *tempPattern = pattern;
int ct = 0;
int numberofcharacters = 0;

    while (  *tempPattern != '\0')
{
        tempPattern++;

      numberofcharacters++;

}

    while ( *text != '\0' && pattern != '\0')
    {
            if ( *pattern == *text)
        {
            pattern++;
            ct++;
            text++;

        }
            else
        {
           text++;
        }

    }

        if ( ct == numberofcharacters )
    {
            return(1);
    }
        else
    {
            return(0);
    }


}

这是有问题的第一个循环,因为正如评论部分所提到的,它完全将变量模式更改为空,因此没有什么可比较的。此代码运行良好。

【讨论】:

    【解决方案2】:

    假设这是一个家庭作业问题,或者您正在学习考虑这个版本的初学者:

    int isIncluded ( char *text, char *pattern );   /* this is ok */
    /* 
       can also write it this way
    
       int isIncluded ( char text[], char pattern[] );
    */
    
    int main ( void )
    {
    
        char text[30];
        char pattern[30];
        int result;
    
        printf(" Please introduce your text \n");
        scanf("%s", text);        /* don't use &text here */
    
        printf(" Please introduce the pattern you are looking for \n");
        scanf("%s", pattern);    /* don't use &pattern here */
    
        /* don't pass a pointer to a pointer, the strings text and pattern are already pointers, you passing &text would mean isIncluded( char **text ) */
    
        result = isIncluded( text, pattern );
    
        if ( result == 1 )
        {
            printf(" Your pattern has been found in your text \n " ) ;
        }
        else if ( result == 0 )
        {
            printf(" no substring found \n " ) ;
        }
        else
        {
            /* don't overlook possibilities of missing simple stuff by not making use of else with  if statements */
            printf(" Error: value of result is %d\n", result );
        }
    }
    

    我还没有研究为什么 isIncluded 总是返回零,但是将指针传递给指向它的指针并没有帮助。

    如果你很好,也许我会写部分代码让你开始, 但是你想做的已经完全由strstr()提供的# include &lt;string.h&gt;完成了

    另外,定义 char text[30]定义指向类型字符的指针 称为文本相同,但另外它在内存中保留空间来保存 [n] 个字符你说是 30 岁。在这种情况下,除了在执行 text[3] 与 @987654328 时实际发生的情况之外,了解 定义声明 之间的区别可能会有所帮助@ 两者都是有效的,明确的,并且做同样的事情。

    【讨论】:

    • 数组不是指针;指针不是数组。我建议您阅读c-faq 的第 6 节。
    【解决方案3】:

    在您的isIncluded 函数中,首先计算pattern 的长度,您更改pattern,然后不是第一个,您可以使用strlen 等其他方式计算长度,但如果您想用这样,您应该创建一个新变量为temp 并更改temp 变量。

    在第二个while循环中模式结束的情况下,此函数的另一个问题应该使用*pattern != '\0'而不是pattern != '\0'

    当你想调用一个带有char*参数的函数并且你有字符数组时,你应该发送数组并且&amp;text类型是char**

    最后你想要什么:

    #include <stdio.h>
    
    #include <stdlib.h>
    
    int isIncluded(char *text, char* pattern);
    
    int main()
    {
    
        char text[30];
        char pattern[30]; int result;
    
        printf(" Please introduce your text \n");
        scanf("%s", &text);
    
        printf(" Please introduce the pattern you are looking for \n");
        scanf("%s", &pattern);
    
    
    
        result = isIncluded(text, pattern);
    
        if (result == 1)
        {
    
            printf(" Your pattern has been found in your text \n ");
        }
        if (result == 0)
        {
    
            printf(" no substring found \n ");
        }
    
    
    
    
    
    }
    
    
    int isIncluded(char *text, char* pattern)
    {
    
        int ct = 0;
        int numberofcharacters = 0;
        char *temp = pattern;        //<- define new variable 
    
        while (*temp != '\0')
        {
            temp++;
            numberofcharacters++;
    
        }
    
        while (*text != '\0' && *pattern != '\0')
        {
            if (*pattern == *text)
            {
                pattern++;
                ct++;
                text++;
    
            }
            else
            {
                text++;
            }
    
        }
    
        if (ct == numberofcharacters)
        {
            return(1);
        }
        else
        {
            return(0);
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 2012-09-06
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多