无法检查空字符串
Not able to check an empty string
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int t,l;
char a[22];
a[0]='0';
for(t=1;t<=20;t++)
{
l=1;
scanf("%s",a+1);
if(strlen(a)>1)
l=strlen(a);
printf("%dn",l-1);
}
return 0;
}
当我输入长度为>= 1 的任何字符串时,我得到了字符串长度的正确答案,但是当只使用击键输入或空格时,它不会打印 0。由于字符串为空(仅包含零),因此它应该打印 0(l-1=0),因为如果条件为假。
使用 fgets
.这个答案是给C的。
改变
scanf("%s",a+1);
自
fgets (a, sizeof (a), stdin);
相关文章:
- 如何检查一个c++字符串中有多少相同的字符/数字
- 在编译时检查字符串文本的长度
- 检查字符串是否"null" C++
- 检查连续对的字符串C++
- 我应该如何解析和检查字符串?
- 检查字符串是否为整数
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符
- 如何检查模板类中的变量是否为字符串类型?
- 检查数组中是否有字符串中的值,如果没有,则添加它
- C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
- 字符串变成链表,然后检查括号余额
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- 如何检查字符串中是否有重复的负号?例如 --1
- 如何检查字符串是否正确以将其转换为 int?
- 字符串检查总是返回假的Palidrome
- 自定义 {fmt} 格式化函数,具有编译时格式字符串检查功能
- 是否可以使用字符串检查预处理器定义
- NULL /空字符串检查:CPU开销
- 使用数组和无指针创建回文字符串检查器C++
- 使用constexpr进行基本编译时格式字符串检查