[程序员面试题精选100题]13.第一个只出现一次的字符
【题目】
在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
【分析】
【代码】
/*********************************
* 日期:2013-12-23
* 作者:SJF0115
* 题目: 13.第一个只出现一次的字符
* 来源:微软
* 分类:程序员面试题精选100题
**********************************/
#include <iostream>
#include <cstring>
using namespace std;
// 第一个只出现一次的字符
char FirstNotRepeatingChar(string str){
int count[256];
int len = str.length();
if(len <= 0){
return '\0';
}
// 初始化为0
memset(count,0,sizeof(count[0])*256);
// 统计字符个数
for(int i = 0;i < len;i++){
count[str[i]]++;
}//for
// 寻找第一个只出现一次的字符
for(int i = 0;i < len;i++){
if(count[str[i]] == 1){
return str[i];
}//if
}//for
return '\0';
}
int main(){
string str("abbacccdeff");
cout<<FirstNotRepeatingChar(str)<<endl;
return 0;
}