return first non repeating character in a string

Posted by Amm Sokun on Stack Overflow See other posts from Stack Overflow or by Amm Sokun
Published on 2011-06-28T05:46:28Z Indexed on 2011/06/28 8:22 UTC
Read the original article Hit count: 278

Filed under:
|

I had to solve this question in which given a string, i had to return the first non repeating character present in the string.

I solved it using hashtable and wrote a method which takes a constant reference to the string and returns the first non repeating character. However, when there is no non repeating character present in the string, i return -1 and in the main program i check as follows

char c = firstNonRepeating( word );
if (static_cast<int> (c) == -1)
     cout<<"no non repeating character present\n";
else
     cout<<c<<endl;

is that the correct way to return -1 when required character is not present?

© Stack Overflow or respective owner

Related posts about c++

Related posts about algorithm