找第一个只出现一次的字符 c程序设计

描述给定t个字符串,这个字符串只可能由26个小写字母组成
请你找到第一个仅出现一次的字符,如果没有符合要求的字符,就输出no。

关于输入第一行是t,接下来是t个字符串,每个字符串长度小于100000关于输出你的输出需要由t行组成。
对于每个字符串,输出第一个仅出现一次的字符,没有输出no。

#include<stdio.h>
char oneChar(char* str){
char deq[26]; //记录出现字符的顺序
unsigned int i = 0,j=0;
unsigned char letter[26]; //统计字符出现的次数
memset(letter,0,26); //初始字符出现次数为0
while(*str!='\0'){
letter[*str-'a']++;
if(letter[*str-'a']==1)deq[i++]=*str;
str++;
}
for(;j<i;j++)
if(letter[deq[j]-'a']==1)return deq[j];
if(i==j) return 0;
}
int main(){
char * test = "very very good!"; //测试字符串
char ch = oneChar(test);
if(ch==0) printf("no\n");
else printf("%c\n",ch);
}输出结果为: g
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-13
我把关键算法写一下,找一个字符串中第一个出现一次的字符:改了一下,可以实际运行 char source[]="where are you?what are you doing?";
char* str=source;
char* tmp=str;
int c=0;
char ch=*str;
while (ch){
if (ch!=1){//已被验证重复的字符
c=0;
tmp=str+1;
while (*tmp++) if (*tmp==ch) {*tmp=1;c=1;};
if (c==0) break;//表示找到了这个符合条件的字符
}
ch=*(++str);
}
printf("%c",ch);
第2个回答  2021-03-30

信息学奥赛 1130-找第一个只出现一次的字符

相似回答