C语言 getline 检查行是否为空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3981510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getline check if line is whitespace
提问by Matt
Is there an easy way to check if a line is empty. So i want to check if it contains any white space such as \r\n\t and spaces.
有没有一种简单的方法来检查一行是否为空。所以我想检查它是否包含任何空格,例如 \r\n\t 和空格。
Thanks
谢谢
回答by casablanca
You can use the isspacefunction in a loop to check if all characters are whitespace:
您可以isspace在循环中使用该函数来检查所有字符是否都是空格:
int is_empty(const char *s) {
while (*s != 'bool isempty(const char *s)
{
while (*s) {
if (!isspace(*s))
return false;
s++;
}
return true;
}
') {
if (!isspace((unsigned char)*s))
return 0;
s++;
}
return 1;
}
This function will return 0 if any character is not whitespace (i.e. line is not empty), or 1 otherwise.
如果任何字符不是空格(即行不为空),则此函数将返回 0,否则返回 1。
回答by Geoff Reedy
If a string sconsists only of white space characters then strspn(s, " \r\n\t")will return the length of the string. Therefore a simple way to check is strspn(s, " \r\n\t") == strlen(s)but this will traverse the string twice. You can also write a simple function that would traverse at the string only once:
如果字符串s仅由空格字符组成,strspn(s, " \r\n\t")则将返回字符串的长度。因此,一种简单的检查方法是strspn(s, " \r\n\t") == strlen(s)但这将遍历字符串两次。您还可以编写一个只遍历字符串一次的简单函数:
int is_empty(const char *s) {
while ( isspace( (unsigned char)*s) )
s++;
return *s == 'int is_empty(const char *s)
{
while ( isspace(*s) && s++ );
return !*s;
}
' ? 1 : 0;
}
回答by Nyan
I won't check for '\0' since '\0' is not space and the loop will end there.
我不会检查 '\0' 因为 '\0' 不是空格并且循环将在那里结束。
std::string str = " ";
std::all_of(str.begin(), str.end(), isspace); //this returns true in this case
回答by gon1332
My suggestion would be:
我的建议是:
std::all_of(str.begin(), str.end(), [](const char& c) { return c == ' '; });
with a working example.
用一个工作示例。
- Loops over the characters of the string and stops when
- either a non-space character was found,
- or nul character was visited.
- Where the string pointer has stopped, check if the contains of the string is the nul character.
- 循环遍历字符串的字符并在以下时间停止
- 要么找到了非空格字符,
- 或访问了空字符。
- 在字符串指针停止的地方,检查字符串的包含是否为空字符。
In matter of complexity, it's linear with O(n), where n the size of the input string.
在复杂性方面,它与 O(n) 成线性关系,其中 n 是输入字符串的大小。
回答by Jonathan.
For C++11 you can check is a string is whitespace using std::all_ofand isspace(isspace checks for spaces, tabs, newline, vertical tab, feed and carriage return:
对于 C++11,您可以使用std::all_ofandisspace检查字符串是否为空格(isspace 检查空格、制表符、换行符、垂直制表符、提要和回车:
#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;
}
if you really only want to check for the character space then:
如果你真的只想检查字符空间,那么:
bool onlyspaces = true;
for(char *y = x; *y != '##代码##'; ++y)
{
if(*y != '\n') if(*y != '\t') if(*y != '\r') if(*y != ' ') { onlyspaces = false; break; }
}
回答by Rizo
Consider the following example:
考虑以下示例:
##代码##回答by Benoit
Given a char *x=" ";here is what I can suggest:
鉴于char *x=" ";这里是我可以建议:

