【发布时间】:2017-02-15 06:07:19
【问题描述】:
我是 C 语言的新手,谁能帮忙找出我在实现一个计算空格、制表符和换行符的程序时做错了什么。
代码:
#include <stdio.h>
//Write a program to count blanks, tabs, and newlines.
int main(){
char str[100];
printf("Enter the text");
scanf("%s",&str);
int space=0;
int tabs=0;
int newLine=0;
int i=0;
while(str[i]!='\0'){
if (str[i] == ' ')
space++;
if (str[i] == '\t')
tabs++;
if (str[i] == '\n')
newLine++;
i++;
}
printf("space:%d\ntabs:%d\nnew line:%d",space,tabs,newLine);
return 0;
}
【问题讨论】:
-
如果我想从控制台而不是文件获取输入怎么办? @xing
-
我可以使用getchar()吗?如果是,那么如何?因为我已经尝试过了,但它没有给出正确的输出
-
谢谢@xing
-
@xing - 如果
ch应该是int用于getchar