【问题标题】:getline check if line is whitespacegetline 检查行是否为空格
【发布时间】:2011-04-28 05:55:09
【问题描述】:

有没有一种简单的方法来检查一行是否为空。所以我想检查它是否包含任何空格,例如 \r\n\t 和空格。

谢谢

【问题讨论】:

  • 但是isspace()的返回值取决于安装的c语言环境。因此,根据它可以为换行符或制表符返回 false。

标签: c whitespace getline


【解决方案1】:

您可以在循环中使用isspace 函数来检查是否所有字符都是空格:

int is_empty(const char *s) {
  while (*s != '\0') {
    if (!isspace((unsigned char)*s))
      return 0;
    s++;
  }
  return 1;
}

如果任何字符不是空格(即行不为空),此函数将返回 0,否则返回 1。

【讨论】:

  • isspace 的参数应转换为 unsigned char(is* 函数“不喜欢”负输入和 char 可能已签名):isspace((unsigned char)*s)
【解决方案2】:

如果字符串s 仅包含空格字符,则strspn(s, " \r\n\t") 将返回字符串的长度。因此,一个简单的检查方法是strspn(s, " \r\n\t") == strlen(s),但这将遍历字符串两次。您还可以编写一个仅在字符串处遍历一次的简单函数:

bool isempty(const char *s)
{
  while (*s) {
    if (!isspace(*s))
      return false;
    s++;
  }
  return true;
}

【讨论】:

    【解决方案3】:

    我不会检查 '\0',因为 '\0' 不是空格,循环将在那里结束。

    int is_empty(const char *s) {
      while ( isspace( (unsigned char)*s) )
              s++;
      return *s == '\0' ? 1 : 0;
    }
    

    【讨论】:

    • *s == '\0' ? 1 : 0 可以简化为*s == '\0'
    【解决方案4】:

    鉴于char *x=" ";,这是我可以建议的:

    bool onlyspaces = true;
    for(char *y = x; *y != '\0'; ++y)
    {
        if(*y != '\n') if(*y != '\t') if(*y != '\r') if(*y != ' ') { onlyspaces = false; break; }
    }
    

    【讨论】:

      【解决方案5】:

      考虑以下示例:

      #include <iostream>
      #include <ctype.h>
      
      bool is_blank(const char* c)
      {
          while (*c)
          {
             if (!isspace(*c))
                 return false;
             c++;
          }
          return false;
      }
      
      int main ()
      {
        char name[256];
      
        std::cout << "Enter your name: ";
        std::cin.getline (name,256);
        if (is_blank(name))
             std::cout << "No name was given." << std:.endl;
      
      
        return 0;
      }
      

      【讨论】:

      • str, *c, c 是哪个? :-)
      • @pmg:只有str 是错误的。 *cc 的值,所以没关系!不过谢谢!
      【解决方案6】:

      我的建议是:

      int is_empty(const char *s)
      {
          while ( isspace(*s) && s++ );
          return !*s;
      }
      

      working example

      1. 循环遍历字符串的字符并在以下情况下停止
        • 找到非空格字符,
        • 或访问了 nul 字符。
      2. 在字符串指针停止的地方,检查字符串的包含是否为空字符。

      在复杂度方面,它与 O(n) 成线性关系,其中 n 是输入字符串的大小。

      【讨论】:

        【解决方案7】:

        对于 C++11,您可以使用 std::all_ofisspace 检查字符串是否为空格(isspace 检查空格、制表符、换行符、垂直制表符、提要和回车符:

        std::string str = "     ";
        std::all_of(str.begin(), str.end(), isspace); //this returns true in this case
        

        如果你真的只想检查字符空间,那么:

        std::all_of(str.begin(), str.end(), [](const char& c) { return c == ' '; });
        

        【讨论】:

          【解决方案8】:

          这可以通过 strspn 一次性完成(只是 bool 表达式):

          char *s;
          ...
          ( s[ strspn(s, " \r\n\t") ] == '\0' )
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-08-19
            • 2014-04-11
            • 1970-01-01
            • 2020-07-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多