#include <stdio.h>
void count_w(char* source,int* sum_num,int* sum_char,int* sum_space)
{
while(*source!='\0')
{
if(*source>='a' && *source<='z') (*sum_char)++;
if(*source>='A' && *source<='Z') (*sum_char)++;
if(*source>='0' && *source<='9') (*sum_num)++;
if(*source==0x20) (*sum_space)++;
source++;
}
}
int main(void)
{
int sum_num=0,sum_char=0,sum_space=0;
char* dest="122314dslkjfaKSLDJ 23sdfa";
count_w(dest,&sum_num,&sum_char,&sum_space);
printf("sum of num is %d\n",sum_num);
printf("sum of char is %d\n",sum_char);
printf("sum of space is %d\n",sum_space);
return 0;
}
-------------------------------------------------------------------------
另一种方法 类似数组的方法 和上面的一种 其实是同宗
#include <stdio.h>
int* count_w(char* source,int* ret)
{
while(*source!='\0')
{
if(*source>='a' && *source<='z') (ret[0])++;
if(*source>='A' && *source<='Z') (ret[0])++;
if(*source>='0' && *source<='9') (ret[1])++;
if(*source==0x20) (ret[2])++;
source++;
}
return ret;
}
int main(void)
{
int sum_num[3]={0};
char* dest="122314dslkjfaKSLDJ 23sdfa";
count_w(dest,&sum_num[0]);
printf("sum of num is %d\n",sum_num[1]);
printf("sum of char is %d\n",sum_num[0]);
printf("sum of space is %d\n",sum_num[2]);
return 0;
}