当前位置: 代码迷 >> C语言 >> [求助] WHILE循环结构
  详细解决方案

[求助] WHILE循环结构

热度:172   发布时间:2006-06-28 23:49:09.0
[求助] WHILE循环结构
输入一行字符 分别统计出其中文字母,空格 数字和其他字符的个数

include<stdio.h>

void main()
{
char ch;
int x,y,j,k; //分别表示数字 字母 空格 其他字符的个数//
printf(" 请输入一行字符:\n");

while((ch=getchar())!='\n')

这里的if 语句不会写请帮助 啊 谢谢了


}
搜索更多相关的解决方案: WHILE  结构  

----------------解决方案--------------------------------------------------------
please wait
----------------解决方案--------------------------------------------------------

谢谢大哥啊 在线等


----------------解决方案--------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main(void)
{
char line[BUFSIZ];
int nalpha = 0, ndigit = 0, nspace = 0, nother = 0, i;

printf("Enter line: ");
fgets(line, BUFSIZ, stdin);

for (i = 0; line[i] != '\0'; i++)
{
if (isalpha(line[i]))
{
nalpha++;
}
else if (isdigit(line[i]))
{
ndigit++;
}
else if (isspace(line[i]))
{
nspace++;
}
else
{
nother++;
}
}


printf("nalpha = %d, ndigit = %d, nspace = %d, nother = %d\n", nalpha, ndigit, nspace, nother);

exit(0);
}





----------------解决方案--------------------------------------------------------
还没来得及看 先说个谢谢

谢谢好心人

你的QQ方便告诉我吗?
论坛短信告诉我也可以
谢谢了
----------------解决方案--------------------------------------------------------
Sorry, I don't used TM

----------------解决方案--------------------------------------------------------
MSN 或者其他的  非要用E文交流吗?~
----------------解决方案--------------------------------------------------------

TM是什么哇???


----------------解决方案--------------------------------------------------------
上边大哥有点小问题,主判断不是'\0'终止符,应该是'\n'.
还有用fgets好象用大了点,直接用gets就好了.
----------------解决方案--------------------------------------------------------

我的代码:


//统计英文字母,space,number,其它字符
#include <stdio.h>
#include <ctype.h>
void main()
{
int cnt_alpha = 0, cnt_space = 0, cnt_num = 0, cnt_others = 0;
char c;
printf("输入一行字符:\n");
while((c=getchar()) != '\n')
{
if(isalpha(c)) cnt_alpha++;
else if(isspace(c)) cnt_space++;
else if(isdigit(c)) cnt_num++;
else cnt_others++;
}
printf("英文字母%d,space%d个,number%d个,others%d个 \n",cnt_alpha,cnt_space,cnt_num,cnt_others);
}


----------------解决方案--------------------------------------------------------
  相关解决方案