梦幻西游手游49you版下:C语言程序问题帮我下

来源:百度文库 编辑:高考问答 时间:2024/05/07 16:56:19
题目是这样的 输入一行字符 分别统计出其中英文字母;空格 数字 和其他字符的个数.

求高手帮我下 谢谢
我想要用while 循环来 实现
可以帮我吗

#include "stdio.h"

main()
{
char c;
int letters = 0, space = 0, digit = 0, others = 0;
printf( "please input some characters\n" );
while ((c = getchar()) != '\n')
{
if (c > = 'a' && c <= 'z' || c > = 'A' && c <= 'Z')
letters++;
else if (c == ' ')
space++;
else if (c > = '0' && c <= '9')
digit++;
else
others++;
}
printf( "all in all:char=%d space=%d digit=%d others=%d\n" , letters, space, digit, others);
}

//请问错什么地方了,我已经在.net平台上测试过了
#include<stdio.h>
#include <ctype.h>
char s[1000];
int len=0;

int main()
{
int alpha=0;
int blank=0;
int other=0;
int num=0;

//get the string
char ch;
while(scanf("%c",&ch)==1 && ch!='\n')
s[len++]=ch;
s[len]='\0';

for(int i=0 ; i<len ; i++)
{
if(isalpha(s[i])) alpha++;
else if(s[i] == ' ') blank++;
else if(isdigit(s[i])) num++;
else other++;
}

printf("%d %d %d %d\n",alpha,blank,other,num);
}

zzsheng_1985 肯定不对
还是不对

习惯问题

#include "stdafx.h"
#include <string.h>

int main(int argc, char* argv[])
{
int num_int=0,num_char=0,num_space=0,num_others=0;
char str[10]="abc124 e5";
for(int i=0;i<strlen(str);i++)
{
if(str[i]>=48 && str[i]<=57)
num_int++;
else if(str[i]>=65 && str[i]<=90)
num_char++;
else if(str[i]>=97 && str[i]<=122)
num_char++;
else if(str[i]=32)
num_space++;
else
num_others++;
}

printf("数字%d个,字母%d个,空格%d,其它%d个.\n",num_int,num_char,num_space,num_others);
printf("Hello World!\n");
return 0;
}
输出:
数字4个,字母4个,空格1,其它0个.
Hello World!
Press any key to continue

百度贴吧里面有这方面的题和答案,100多道,一搜就能找到。

#include<stdio.h>
main(){
int c,e=0,k=0,s=0,q=0;
clrscr();
printf(\"enter:\\n\");
while((c=getchar())!=\'\\n\')
{if(c<=\'z\'&&c>=\'a\'||c<=\'Z\'&&c>=\'A\') e++;
else if(c==\' \') k++;
else if(c<=\'9\'&&c>=\'0\') s++;
else q++;
}
printf(\"\\ne=%d,k=%d,s=%d,q=%d\\n\",e,k,s,q);
getch();
}