int count0=0,count1=0,cnt=0;
char str[200];
char ch;
ch= getchar();
while(ch!='\0')
{
str[cnt]=ch;
cnt++;
}
str[cnt]='\0';
printf("%s",str);
预期输出:
shubham
输出:
Your code didn't print anything.
输入:
shubham
最佳答案
您只接受一个字符,您应该将 while()
替换为 do-while()
。
int ch;
do
{
ch= getchar();
if(ch == EOF)
{
str[cnt] = '\0';
break;
}
else
str[cnt] = ch;
cnt++;
}while(ch != '\0');
上面的循环应该可以解决您面临的问题。您需要在字符串末尾使用 ctrl
+@
输入 NULL 终止符。
关于c - 我的程序不接受使用 getchar 的 c 中的简单字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47068194/