Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- bool firstUniqueChar(std::string &, char &);
- int main()
- {
- //line read from input
- std::string line;
- //word in read line
- std::string word;
- //the result of processing words, which contains all first unique characters in words
- std::string half_result;
- int pos;
- //the input can consist of one or more lines, so we'll process them all
- for(;std::getline(std::cin, line);)
- {
- //find the position of next ' '
- //if no matches were found, find returns string::npos
- while((pos = line.find(" ")) != std::string::npos)
- {
- //current word
- word = line.substr(0, pos);
- //cut this word including first ' ' from input string
- line.erase(0, pos + 1);
- //the input word can be empty. in this case we'll not process it
- if(!word.empty())
- {
- char c;
- if(firstUniqueChar(word, c))
- half_result.append(1, c);
- }
- }
- //the rest of the line contains last word
- //it also can be empty
- if(!line.empty())
- {
- char c;
- if(firstUniqueChar(word, c))
- half_result.append(1, c);
- }
- }
- char c;
- if(!half_result.empty() && firstUniqueChar(half_result, c))
- std::cout<<std::endl<<c<<std::endl;
- else
- std::cout<<"<nothing>"<<std::endl;
- return 0;
- }
- bool firstUniqueChar(std::string &word, char &c)
- {
- for(std::string::iterator it = word.begin(); it != word.end(); ++it)
- {
- //find occurence of current char in string Before current position
- int posB = (std::distance(word.begin(), it)) != 0 ? word.rfind(*it, std::distance(word.begin(), it) - 1) : std::string::npos;
- //find occurence of current char in string After current position
- int posA = (std::distance(word.begin(), it)) != word.size() - 1 ? word.find(*it, std::distance(word.begin(), it) + 1) : std::string::npos;
- if(posA == std::string::npos && posB == std::string::npos){
- c = *it;
- return true;
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement