空白在C中是如何定义的?
我想使用空白在C中是如何定义的?,c,whitespace,C,Whitespace,我想使用scanf获取一个字符串作为输入,如果字符串只是一个空格或空白,我必须打印错误消息 这就是我试图做的: char string1[20] scanf("%s",string1) if(string1=='') print error message 但这不起作用,实际上我没想到它会起作用,因为string1是一个字符数组 有什么提示吗?您应该注意,scanf函数永远不会扫描只有空格的字符串。相反,检查函数的返回值,如果它(在您的例子中)小于1,则无法读取字符串 您可能希望使用读
scanf
获取一个字符串作为输入,如果字符串只是一个空格或空白,我必须打印错误消息
这就是我试图做的:
char string1[20]
scanf("%s",string1)
if(string1=='')
print error message
但这不起作用,实际上我没想到它会起作用,因为string1
是一个字符数组
有什么提示吗?您应该注意,
scanf
函数永远不会扫描只有空格的字符串。相反,检查函数的返回值,如果它(在您的例子中)小于1,则无法读取字符串
您可能希望使用读取一行,删除尾随的换行符,然后检查字符串中的每个字符是否都是空格(使用函数) 像这样:
char string1[20];
if (fgets(string1, sizeof(string1), stdin) != NULL)
{
/* Remove the trailing newline left by the `fgets` function */
/* This is done by changing the last character (which is the newline)
* to the string terminator character
*/
string1[strlen(string1) - 1] = '\0';
/* Now "remove" leading whitespace */
for (char *ptr = string1; *ptr != '\0' && isspace(*ptr); ++ptr)
;
/* After the above loop, `*ptr` will either be the string terminator,
* in which case the string was all blanks, or else `ptr` will be
* pointing to the actual text
*/
if (*ptr == '\0')
{
/* Error, string was empty */
}
else
{
/* Success, `ptr` points to the input */
/* Note: The string may contain trailing whitespace */
}
}
您应该注意,
scanf
函数永远不会扫描只有空格的字符串。相反,检查函数的返回值,如果它(在您的例子中)小于1,则无法读取字符串
您可能希望使用读取一行,删除尾随的换行符,然后检查字符串中的每个字符是否都是空格(使用函数) 像这样:
char string1[20];
if (fgets(string1, sizeof(string1), stdin) != NULL)
{
/* Remove the trailing newline left by the `fgets` function */
/* This is done by changing the last character (which is the newline)
* to the string terminator character
*/
string1[strlen(string1) - 1] = '\0';
/* Now "remove" leading whitespace */
for (char *ptr = string1; *ptr != '\0' && isspace(*ptr); ++ptr)
;
/* After the above loop, `*ptr` will either be the string terminator,
* in which case the string was all blanks, or else `ptr` will be
* pointing to the actual text
*/
if (*ptr == '\0')
{
/* Error, string was empty */
}
else
{
/* Success, `ptr` points to the input */
/* Note: The string may contain trailing whitespace */
}
}
首先,
scanf
将跳过任何空格,如果您在格式说明符之前放置空格(或其他空格字符,如'\n'
或'\t'
),如scanf(“%s”,&str)
第二,<代码>(Strug1=='')< /> >将比较char指针<代码> Strug1与空白char <代码> \代码> > < <强>绝对不为< /强>,因为现有变量的地址将为非null。也就是说,在C语言中没有像
'
那样的“空白”字符。您需要获取行输入并首先解析它是一个空行还是只包含空格,scanf
将跳过任何空格如果您放入空格(或其他空白字符,如'\n'
或'\t'
)在格式说明符之前,如scanf(“%s”,&str)
第二,<代码>(Strug1=='')< /> >将比较char指针<代码> Strug1与空白char <代码> \代码> > < <强>绝对不为< /强>,因为现有变量的地址将为非null。也就是说,在C中没有像
'
那样的“空白”字符。您需要获取行输入并解析它是一个空白行还是只包含空格
选择格式指定“%s
”、“%d
”、“%f
”跳过前导空格。(空白)。其他格式指定,如“
%c
”、“%[]
”、“%n
”不跳过前导空格
按行扫描并查找空格。(string1可能包含空格)
charstring1[20];
//最多扫描19个非换行符,然后扫描下一个字符(假定为\n)
int result=scanf(“%19[^\n]%*c”,string1);
如果(结果<0)处理_IOError_或_EOF();
否则,如果(结果==0)处理输入的内容();
否则{
常量char*p=string1;
而(isspace(*p))p++;
如果(*p=='\0')
打印错误消息
}
scanf()
并不总是跳过前导空格
选择格式指定“%s
”、“%d
”、“%f
”跳过前导空格。(空白)。其他格式指定,如“
%c
”、“%[]
”、“%n
”不跳过前导空格
按行扫描并查找空格。(string1可能包含空格)
charstring1[20];
//最多扫描19个非换行符,然后扫描下一个字符(假定为\n)
int result=scanf(“%19[^\n]%*c”,string1);
如果(结果<0)处理_IOError_或_EOF();
否则,如果(结果==0)处理输入的内容();
否则{
常量char*p=string1;
而(isspace(*p))p++;
如果(*p=='\0')
打印错误消息
}
检查函数。“
是一个定义错误的实体。如果引号之间有一些字符,它将是一个int。您无法将字符数组与int进行比较。如果要确定string1
是空字符串,您需要检查string1[0]='\0'
是否检查函数。'
是一个定义错误的实体。如果引号之间有一些字符,它将是一个int。您无法将字符数组与int进行比较。如果要确定string1
是空字符串,您需要检查string1[0]='\0'
检查函数返回值的含义是什么?哪个函数?@someone调用scanf
。您可能需要阅读,例如.。注意:尽管可能性很小,string1[strlen(string1)-1]='\0'
如果字符串的第一个字节是\0
,可能会严重失败。示例:带有嵌入式NUL的管道输入。检查函数返回值的含义是什么?哪个函数?@someone调用scanf
。您可能需要阅读,例如.。注意:尽管可能性很小,string1[strlen(string1)-1]='\0'
如果字符串的第一个字节是\0
,可能会严重失败。示例:带有嵌入式NUL的管道输入。