#include<cstdio>
#include<cstdlib>
#include<cstring>
//char alp[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
//char alp[27] = "abcdefghijklmnopqrstuvwxyz";之所以27,是因为'\0'也是一个字符
char vis[26] = {0};
int getCount(char *str , int len)
{
int _count = 0;
memset(vis , 0 ,sizeof(char)*26);//vis数组是全局变量,如果不初始化,则会将结果累计
for(int i = 0 ; i < len ; i++)
{
vis[str[i] - 'a'] = 1;//输入字符串全是小写字母,其差刚好在0到25之间
}
for(int m = 0 ; m < 26 ; m++)
{
if(vis[m])
_count++;
}
return _count;
}
int main()
{
int res1 = getCount("abcd", 4);
int res2 = getCount("aaaa", 4);
printf("%d %d\n" , res1 , res2);
/*char str[50];
gets(str);
int res = getCount(str , strlen(str));
printf("%d" , res);*/
return 0;
}
统计一个全是小写字母的字符串中字母的种类数
最新推荐文章于 2021-12-05 13:45:51 发布