编写一个函数,由实参数传来一个字符串,统计此字符串中字母,数字,空格和其他字符的个数,在 主函数中输入字

的个数,在 主函数中输入字符串以及输出上述的结果. 急!!!!!

#include <stdio.h>

#include <string.h>

int letter,number,blank,other;

void count(char str[])

{

int i;

for(i=0;str[i]!='\0';i++)

{

if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))

letter++;

else if(str[i]>='0'&&str[i]<='9')

number++;

else if(str[i]==' ')

blank++;

else

other++;

}

int main()

{

char a[80];

gets(a);

puts(a);

strcat(a,"\0");

letter=0;

number=0;

blank=0;

other=0;

count(a);

printf("\n%5d,%5d,%5d,%5d\n",letter,number,blank,other);  

return 0;

}

扩展资料:

C语言需要说明的是:

1、一个C语言源程序可以由一个或多个源文件组成。 

2、每个源文件可由一个或多个函数组成。

3、一个源程序不论由多少个文件组成,都有一个且只能有一个main函数,即主函数。是整个程序的入口。 

4、源程序中可以有预处理命令(包括include 命令,ifdef、ifndef命令、define命令),预处理命令通常应放在源文件或源程序的最前面。

5、每一个说明,每一个语句都必须以分号结尾。但预处理命令,函数头和花括号“}”之后不能加分号。结构体、联合体、枚举型的声明的“}”后要加“ ;”。

6、标识符,关键字之间必须至少加一个空格以示间隔。若已有明显的间隔符,也可不再加空格来间隔。

参考资料:

百度百科-c语言

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-12-16
#include <iostream>
using namespace std;
void main ()
{
char x;
cout<<"请输入一串英文字母、空格、数字和其他的字符:";
int number=0,space=0,others=0,letter=0;
while((x=getchar())!='\n')
{
if((x>=65&&x<=90)||(x<=122&&x>=97)) letter++;
else if (x==32) space++;
else if (x>=48&&x<=57) number++;
else others++;
}
cout<<"number="<<number<<'\n'<<"space="<<space<<'\n'<<"letter="<<letter<<'\n'<<"others="<<others<<endl;
}本回答被提问者采纳
第2个回答  2011-03-23
#include <stdio.h>
main() /* count digits, white space, others */
{
int c, i, nwhite, nother, ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; i++)
ndigit[i] = 0;
while ((c = getchar()) != EOF) {
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
ndigit[c-'0']++;
break;
case ' ':
case '\n':
case '\t':
nwhite++;
break;
default:
nother++;
break;
}
}
printf("digits =");
for (i = 0; i < 10; i++)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n",
nwhite, nother);
return 0;
}
第3个回答  2011-03-12
/* Note:Your choice is C IDE */
#include "stdio.h"
int num,letter,blank,other;
void main()
{
void total(char str1[20]);
char str[40];
gets(str);
total(str);
printf("%5d %5d %5d %5d",letter,num,blank,other);

}
void total(char str1[20])
{
int i;
num=0;letter=0;blank=0;other=0;
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]>='A'&&str1[i]<='Z'||str1[i]>='a'&&str1[i]<='z')
letter++;
else if(str1[i]>='0'&&str1[i]<='9')
num++;
else if(str1[i]==' ')
blank++;
else other++;
}
}
相似回答