您的位置:首页 > 职场人生

H面试程序(12): 输出字符串中第一个只出现一次的字母

2013-08-20 08:26 330 查看
题目描述:

若字符串str为'' sbdddsbfc'',则输出 f;

若字符串str为''aabbccdd'',则输出:字符串str中的字符都出现两次以上

#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<malloc.h>

int  find_first_char(char*  str)
{
assert(str);
char  * phash = (char *)malloc(sizeof(int ) *256); //不能是int类型
assert(phash);
memset(phash, 0 , 256);
int i = 0 ;

// 第一遍进行映射
while(str[i] !='\0')
{
phash[str[i]] ++;
i++;
}

//第二遍找到第一个唯一只出现一个的字母
i = 0;
while(str[i] !='\0')
{
if(phash[str[i]] == 1)
return i;
i++;
}
return 0;
}
int main( )
{
char a[ ] = "aabsbccdFCD";
int pos = find_first_char(a);
if( 0 != pos)
{
printf("%c\n",a[pos]);
}
else
printf("没有这样的字母!");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐