In this post, we will discuss how to remove all duplicate characters from a string in C.

To remove all repeated characters we will check for each character if it exists later in the string more than one time, if yes then we will remove all the duplicate characters except the first one. For example popeye will become poey.

To remove duplicate characters in C we need to implement the following steps.

  1. Input the string.
  2. Ckeck if a character matches with another character later in the string i.e str[i] == str[j].
  3. If yes then remove the repeated character by shifting all characters at right side of the duplicate character to one step left. For example, In string “popeye” to remove duplicate ‘p‘ we shift “eye” to one step left, therefore the string will become “poeye“.
  4. Repeat step 3 for all duplicate characters.
  5. Output the final String.

output

In our program we first searched for the duplicate character then we remove it by shifting all the later characters to one step left.

I hope you understood the concept. If you have any doubts or suggestions then please comment. I will be glad to help you.

Leave a Reply