统计一个全是小写字母的字符串中字母的种类数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011386173/article/details/50755893
#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;
}

 
 

猜你喜欢

转载自blog.csdn.net/u011386173/article/details/50755893