Latest :

C Program To Remove Repeated Characters From String | 4 Ways

program to remove all repeated characters from a given string – In this article, we will discuss the multiple methods to remove all repeated characters from a given string in C programming.

Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.

The methods used to do for C Program To Remove Repeated Characters From String  are as follows:

  • Using Standard Method
  • Using Function

A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.

C Program To Remove Repeated Characters From String

As you can see in the given example, you need to enter the string first up.

The string entered here is “hello world”

It is evident that ‘l’ and ‘o’ are repeated in the string. So, these two alphabets will be omitted out.

The string after removing all the duplicates becomes:

‘helo wrd’

Thus, the different ways to do so in C programming are as follows:

Using Standard Method

  1. Read the entered string and save in the character array s[] using gets(s).

2) temp=1,c=”*”,k=0.

3) Replace all repeated characters with ‘*’ as follows.

a) For loop iterates through the string until the character of the string is null.

b) If the first character not equal to “*”

c) Then compare the first character with the next characters to it. If matched then replace the matched elements with “*”.

Repeat these b,c steps for all elements of the string.

4) Remove all ‘*’  from the string as follows.

for loop iterates through the string until s[i] becomes to null, with the structure for(i=0;s[i];i++).

a) initialize s[i]=s[i+k]

b) If s[i]  is matched with  ‘*’ then increase the k and decrease the index value i.

Here we are shifting the elements to left by removing ‘*’.

5) Print the string.

Output:

Using Function

  1. The main() calls the findduplicate(char *s) to find the repeated characters in the string.

The function findduplicates(char *s)

a) For loop iterates through the string

b) If the element of the string is not a ‘*’ then compare the element with the next elements to it. If matched then replace the matched elements with ‘*’.Then return ‘*’ to main().

Repeat this step for all elements of the string.

2) The main() calls the deleteduplicate(char *s, char c)by passing the string and the duplicate character as arguments to the function to delete the repeated elements from the string.

The function deleteduplicate(char *s,char c)

a) k=0, the for loop iterates through the string

b) If the element matched with ‘*’ then shift the next element to ‘*’, to one position left as s[i]=s[i+k],by increasing k value and decreasing index value i.

Repeat this step for all elements of the string.

Output:

x

Check Also

C Program To Find Reverse Of An Array – C Programs

C program to find the reverse of an array – In this article, we will ...