描述
找出字符串中第一个只出现一次的字符
数据范围:输入的字符串长度满足 1≤n≤1000
输入描述:
输入一个非空字符串
输出描述:
输出第一个只出现一次的字符,如果不存在输出-1
示例1
输入:
asdfasdfo
输出:
o
代码
#include<iostream>
#include<string>
using namespace std;
int main(){
string words;
while(getline(cin,words)){
bool isFound=false;
for(int i=0;i<words.size();i++){
if(words.find_first_of(words.at(i))==words.find_last_of(words.at(i))){//find_first_of正向查找在原字符串中第一个与指定字符串(或字符)中的某个字符匹配的字符,返回它的位置。若查找失败,则返回npos。(npos定义为保证大于任何有效下标的值。)
//find_last_of逆向查找在原字符串中最后一个与指定字符串(或字符)中的某个字符匹配的字符,返回它的位置。若查找失败,则返回npos。(npos定义为保证大于任何有效下标的值。)
cout<<words.at(i)<<endl;
isFound=true;
break;
}
}
if(!isFound)cout<<"-1"<<endl;
}
return 0;
}