首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于字符计数的有关问题

2012-03-20 
关于字符计数的问题。程序代码:C/C++ code#include stdio.h// count characters in input, 1st versionin

关于字符计数的问题。
程序代码:

C/C++ code
#include <stdio.h>// count characters in input, 1st versionint main(void){    long nc;    nc = 0;    while (getchar()!= EOF)        ++nc;        printf("%ld\n", nc);    getchar();    return 0;}

为了给一串字符计数,但是当输入一串字符后ENTER,无法显示数目,要输入两次ctr + z后才显示,这样计数准确吗?怎么样才能回车直接显示字符个数?

[解决办法]
C/C++ code
while (getchar()!= '\n')        ++nc; 

热点排行