Strings in C++ : Finding the first non-repeated character in a string
April 17, 2011 2 Comments
We are given a string, and told to find the first non-repeated character in it. Let’s look at an example: let’s say our string is “alligator”. The first non-repeated character in this string is “i”.
Now, how do we go about finding this first non-repeated character programmatically?
APPROACH 1
There is a very direct algorithm which we can employ — Take the first character of the given string (which is ‘a’ in our example string “alligator”). Compare it with each of the other characters in the string. If that character does not appear anywhere else, return that character and terminate the algorithm. Else, if the character does appear somewhere else in the string (as in the case of the ‘a’ in “alligator”), go to the 2nd character of the string, and repeat the same process (i.e. check if the 2nd character of the string gets repeated anywhere). Continue this process for each character of the string (i.e. in the k th iteration, we would compare the k th character of the string with each character in the string to determine if it appears anywhere else). If, at the end of the algorithm, we find that all characters get repeated somewhere, i.e. there is no non-repeating character, print a message to that effect.
———
The above algorithm is simple to understand and implement. It will run in time where
is the length of the input string.
——
In pseudocode, the algorithm would look as follows:
——-
Input: String s of length n.
Output: First non-repeated character in s.
Algorithm:
For i going from 0 upto n-1:
flag = true; // flag is a boolean variable initialized to true.
For each j going from 0 upto n-1:
If ( s[i] == s[j] )
flag = false;
End For. //end the inner For loop.
If ( flag == true ) //i.e. if flag is true when we come out of the inner For loop
return s[i]; //character s[i] is the 1st non-repeated character in s.
End For. // end the outer For loop.
//If the algorithm reaches this statement, it means that there is no non-repeated character in s.
// Hence we return a default character, which would not otherwise be returned.
// We choose the null character — NUL for this purpose.
return NUL;
———
So that was the pseudocode for the algorithm. We can translate the above code into C++ as follows:
———
#include <iostream.h>
// function f takes a string s, and returns the first non-repeated character in s.
// In case there is no non-repeated character, it returns the null character ” (having ASCII value 0)
char f (char *s)
{
//we first check if s is the null string.
if ( !s)
return ”; // return the null character is s is the null string.
int i = 0, j;
bool flag;
while ( s[i] ! = ”)
{
flag = true;
j = 0;
while ( s[j] ! = ”)
{
if ( s[i] == s[j] )
flag = false;
j ++;
}
if (flag)
return s[i];
i++;
}
return ”; //i.e. no non-repeated character was found.
}
————
For each j going from 0 upto n-1:
Thanks for pointing it out. I made the correction in the pseudocode, and also corresponding corrections elsewhere in the post.