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

在主函数中输入字符串及输出上述结果。
提示有错误,麻烦大仙改之。
int alph,digit,space,others;
main()
{
char text[80];
printf("\n输入字符串:\n");
gets(text);
printf("字符串是:");
puts(text);
strcat(text,"\n");
alph=0;
digit=0;
space=0;
others=0;
count(text);
printf("\n%d字母,%d数字,%d空格,%d其它字符\n",alph,digit,space,others);
}
count(str);
char str[];
{
int i;
for(i=0;str[i]!='\n';i++)
{
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
alph++;
else if(str[i]>='0'&&str[i]<='9')
digit++;
else if(str[i]==' ')
space++;
else
others++;
}
}

#include<stdio.h>
#include<string.h>
void inputSTr(char *args){
char temp[100]={0},str[100];
int b=0,c=0,d=0,e=0,i;
for(i=0;i<strlen(args);i++){
strncpy(temp, args+i, 1);
str[i] = args[i];
if(((int)str[i]>=97&&(int)str[i]<=122)||((int)str[i]>=65&&(int)str[i]<=90))
{
b+=1;
}
else if((int)str[i]==32)
{
c+=1;
}
else if(((int)str[i]>=48)&&((int)str[i]<=58))
{
d+=1;
}
else
{
e+=1;
}
}
printf("这个字符串中:字母有%d个,数字有%d个,空格有%d个,其它字符有%d个。\n",b,d,c,e);
}
void main(){
char *x,input[100];
printf("请输入一个字符串:");
gets(input);
x=input;
inputSTr(x);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-12-11

 已经修改过了,主要是scanf输入时,它是以空格为结束标志,即它不能输入空格。要改用gets,它是以回车符为结束标志的。

#include <stdio.h>

#include <string.h>

void main()

{

void shuchu(char x[100]);

char a[100];

gets(a);            /*改了此处*/

shuchu(a);

}


void shuchu(char x[100])

{

int letter, number, space, other;

int n, ch, i;

n = strlen(x);

letter = number = space = other = 0;

for (i = 0; i <= n - 1; i++)

{

ch = x[i];

if ('A' <= ch && ch <= 'Z' || 'a' <= ch && ch <= 'z')

letter++;

else if (ch == ' ')

space++;

else if ('0' <= ch && ch <= '9')

number++;

else

other++;

}

printf("letter=%d\n", letter);

printf("space=%d\n", space);

printf("number=%d\n", number);

printf("other=%d\n", other);

}

第2个回答  推荐于2017-10-13
#include<stdio.h>
#include<string.h>
int alph,digit,space,others;
void count(char str[]);
void main(void)
{
char text[80];
printf("\n输入字符串:\n");
gets(text);
printf("字符串是:");
puts(text);
strcat(text,"\n");
alph=0;
digit=0;
space=0;
others=0;
count(text);
printf("\n%d字母,%d数字,%d空格,%d其它字符\n",alph,digit,space,others);
}
void count(char str[])
{
int i;
for(i=0;str[i]!='\n';i++)
{
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
alph++;
else if(str[i]>='0'&&str[i]<='9')
digit++;
else if(str[i]==' ')
space++;
else
others++;
}
}
你的函数定义不对本回答被提问者采纳
第3个回答  2008-04-22
void func(char s[]){
int i=0,l=0,s=0,n=0,o=0;
while(s[i]){
if (s[i]>='a' && s[i]<='z' || s[i]>='A' && s[i]<='Z') l++;
else if (s[i]>='0' && s[i]<='9') n++;
else if (s[i]==32 || s[i]==9) s++;
else o++;
i++;
}
printf("参数传来的字符串是:%s\n字符串有%d个字符,其中有字母%d个,数字%d个,空格%d个,其它%d个\n",s,i,l,n,s,o);
}
第4个回答  2015-05-17
你统计的结果没有通参数返回,且显示语句也没有数据,修改如下:

#include <stdio.h>
void fac(char str[], int *dx, int *xx, int *sz, int *kg, int *qt);
void main()
{char a[100];
int dx, xx, sz, kg, qt;
printf("请输入一个字符串:");
gets(a);
fac(a, &dx, &xx, &sz, &kg, &qt);
printf("大写字母个数:%d\n", dx);
printf("小写字母个数:%d\n", xx);
printf("数字:%d\n", sz);
printf("空格:%d\n",kg);
printf("其他字符:%d\n", qt);
}

void fac(char str[], int *dx, int *xx, int *sz, int *kg, int *qt)
{
int i, D = 0, d = 0, math = 0, space = 0, others = 0;
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] >= 'a'&&str[i] <= 'z')
D++;
else if (str[i] >= 'A'&&str[i] <= 'Z')
d++;
else if (str[i] >= '0'&&str[i] <= '9')
math++;
else if (str[i] == ' ')
space++;
else
others++;
}
*dx = d; *xx = D; *sz = math; *kg = space; qt = others;
}}
相似回答