YTU 3259: 2.字符串处理

3259: 2.字符串处理


Description

字符串处理,输出字符串不重复字符的数目。

Input

输入字符串(字符总数小于50

Output

不相同字符的个数

Sample Input

abca

Sample Output

3

#include<stdio.h>
#include<string.h>
int main()
{
    int s[999]={0};
    int i,j,k,n=0;
    char ch[99];
    gets(ch);
    int t=strlen(ch);
    for(i=0;i<t;i++)
    {
        if(s[ch[i]]==0)
        {
            n++;
            s[ch[i]]++;
        }
    }
    printf("%d",n);
    return 0;
}
 
 
Another solution.

#include<stdio.h>
#include<string.h>
int main()
{
    int i,j,cnt=0,t;
    char ch[99];
    gets(ch);
    t=strlen(ch);
    for(i=0;i<t;i++)
    {
        for(j=0;j<=i;j++)
            if(ch[i]==ch[j])
                break;
        if(i==j)
            cnt++;
    }
    printf("%d\n",cnt);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/wyh1618/article/details/80464017