编写字符串过滤函数int filterch(char* str),其功能是:只保留str中非零数字字符,并统计其中偶数数字个数。例如:str为ad12AB03#f4()sdf%*5671,过滤后str为12345671,其中偶数数字个数为3。要求:在主函数中初始化一个包含任意大小写英文字母、本人学号及其他字符的字符串(字符串长度不超过20),调用过滤函数filterch,分行输出原始字符串、过滤后的字符串及统计结果。(不允许使用字符串处理库函数)
#include <stdio.h>
#include <stdlib.h>
int filterch(char *str)
{
int i = 0,count=0;
while (str[i]!='\0')
{
if (str[i] <= '9' && str[i]>'0')
{
printf("%c", str[i]);
if((str[i]-'0')%2==0)
{
count++;
}
}
i++;
}
return count;
}
int main()
{
int count = 0;
char a[20] = "ad12A0#)sdf*71";
printf("%s\n", a);
count=filterch(a);
printf("\n%d\n", count);
system("pause");
return 0;
}