现在的位置: 首页 > 综合 > 正文

在一个字符串中找到第一个只出现一次的字符。

2013年06月25日 ⁄ 综合 ⁄ 共 530字 ⁄ 字号 评论关闭
/********************************************
* 在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b
*********************************************/
#include <stdio.h>
#include <string.h>
#define N 256
int charHash[N] = {0};
void initCharHash(const char *s)
{
    while(*s)
    {
        charHash[*s]++;
        s++;
    }
}
char findFirstOnceChar(const char *s)
{
    while(*s)
    {
        if(charHash[*s] == 1)
        {
            return *s;
        }
        s++;
        }
    return 0;
}
int main(void)
{
    char *str = "abaccdeff";
    initCharHash(str);
    printf("%c\n",findFirstOnceChar(str));
    return 0;
}
/*********
b

Process returned 0 (0x0)   execution time : 1.254 s
Press any key to continue.

**********/

抱歉!评论已关闭.