C语言 找第一个只出现一次的字符
Description
给定一个只包含小写字母的字符串,请你找到第一个仅出现一次的字符。如果没有,输出no。
Input
一个字符串,长度小于100000。
Output
输出第一个仅出现一次的字符,若没有则输出no。
Sample Input
abcdbd
Sample Output
c
Source Code
#include <stdio.h>
#include <string.h>
int main()
{
char a[100000];
int c[100000]={0}; //存放每个字符出现的次数,初始时全为0
gets(a);
int length = strlen(a);
for(int i=0;i<length;i++) //遍历字符串中每一个字符
{
for(int j=0;j<length;j++)//遍历第二次让a[j]和a[i]对比
{
if(a[i]==a[j])
c[i]++; //如果相同c[i]++
}
if(c[i]==1)
{
printf("%c\n",a[i]);
return 0;
}
}
printf("no\n");
return 0;
}