【问题标题】:Checking if \n exists in String - C检查字符串中是否存在 \n - C
【发布时间】:2012-03-02 12:46:44
【问题描述】:

我正在尝试比较两个字符串,尽管它们看起来相同,但我没有得到匹配。原来一个字符串包含\n。

所以我的问题是,有没有一种方法可以检查字符串是否包含'\n'? 我正在使用 strcmp 函数;

char *tempData;
char *checkThis;
tempData = "Hello \n";
checkThis = "Hello";
if(strcmp(tempData, checkThis) == 0)
{
  printf("Match");
}

【问题讨论】:

    标签: c string


    【解决方案1】:

    您可以在比较之前去掉空格,这样就不需要检查“\n”。但相反,您可以只比较字符串,假设这是您想要做的。

    This question 有一些关于如何在 C 中做到这一点的答案。

    【讨论】:

    • 是的,擦除空格是描述任务主题的最佳变体。
    【解决方案2】:

    创建您自己的比较函数,忽略 \n 或您传入的任何其他字符:

    int strcmp_ignoring_char(const char* s1, const char* s2, const char ignore)
    {
       while ( *s1 != '\0' && *s1 != '\0' )
       {
          if ( *s1 == ignore )
          {
              s1++;
              continue;
          }
    
         if ( *s2 == ignore )
          {
              s2++;
              continue;
          }
    
          if ( *s1 != *s2 )
              return *s1 > *s2 ? 1 : -1;
    
          s1++;
          s2++;
       }
    
       /* modified to account for trailing ignore chars, as per Lundin comment */
    
       if ( *s1 == '\0' && *s2 == '\0' )
          return 0;
    
       const char* nonEmpty = *s1 == '\0' ? s2 : s1;
       while ( *nonEmpty != '\0' )
           if  ( *nonEmpty++ != ignore )
           return 1;
    
       return 0;
    }
    

    这样你就不会扫描字符串两次了。

    您还可以创建一个忽略字符串而不是单个字符的变体:

    int strcmp_ignoring_char(const char* s1, const char* s2, const char* ignore)
    

    【讨论】:

    • 我喜欢这个想法,但如果找到ignore-char,您的示例将永远不会结束。由于s1 和/或s2 未更新...此外,在您的while 语句中,您检查s1 两次。
    • @Veger 你是完全正确的。我会改变我的答案。
    • 它包含另一个错误:如果例如*s1 == ignore s2 不应该增加(反之亦然)
    • 谢谢。我是C新手,'0是什么? 1' 和 '1: -1' 呢?是说如果 s1 = s2,那么 true (0) else false 1?
    • 这是一个三元如果:boolean_expression ? A : B 如果 boolean_expression 为真则返回 A,否则返回 B。
    【解决方案3】:

    这是我的尝试。我试图让它符合 MISRA-C,除了 C99 功能。

    #include <stdint.h>
    #include <stdbool.h>
    #include <ctype.h>
    #include <stdio.h>
    
    
    int8_t strcmp_ignore_space (const uint8_t* s1, const uint8_t* s2)
    {
      while ((*s1 != '\0') && (*s2 != '\0'))
      {
        bool space1 = isspace(*s1);
        bool space2 = isspace(*s2);
    
        if(space1)
        {
          s1++;
        }
        if(space2)
        {
          s2++;
        }
    
        if (!space1 && !space2)
        {
          if (*s1 != *s2)
          {
            break;
          }
          else
          {
            s1++;
            s2++;
          }
        }
      } // while ((*s1 != '\0') && (*s2 != '\0'))
    
      if(*s1 != '\0')                      // remove trailing white spaces
      {
        while(isspace(*s1))
        {
          s1++;
        }
      }
    
      if(*s2 != '\0')                      // remove trailing white spaces
      {
        while(isspace(*s2))
        {
          s2++;
        }
      }
    
      return (int8_t)( (int16_t)*s1 - (int16_t)*s2 );
    }
    
    
    int main()
    {
      // obscure strings with various white space characters, but otherwise equal
      if(strcmp_ignore_space("  He\vllo \n",
                             "\r He\fll o ") == 0)
      {
        printf("Same\n");
      }
      else
      {
        printf("Different\n");
      }
    
    
      return 0;
    }
    

    【讨论】:

    • 不起作用,它应该返回什么?尝试使用 printf("%d\n", strcmp_ignore_space("Ciao", "ciao")); 并返回 -32
    • @vulkanino The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.。 'C'-'c' = 67-99 = -32。所以它的行为正确。
    • 看起来是一个不错且简单的实现 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2012-05-25
    • 2016-03-27
    相关资源
    最近更新 更多