C 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.
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
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include <stdio.h> #include <string.h> int main() { char s[1000],temp=1,c='*'; int i,j,k=0,n; printf("Enter the string : "); gets(s); for(i=0;s[i];i++) { if(!(s[i]==c)) { for(j=i+1;s[j];j++) { if(s[i]==s[j]) s[j]=c; } } } for(i=0;s[i];i++) { s[i]=s[i+k]; if(s[i]==c) { k++; i--; } } printf("string after removing all duplicates:"); printf("%s",s); return 0; } |
Output:
1 2 |
Enter the string: hello world string after removing all duplicates:helo wrd |
Using Function
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#include <stdio.h> #include <string.h> void deleteduplicate(char *s,char c) { int i,k=0; for(i=0;s[i];i++) { s[i]=s[i+k]; if(s[i]==c) { k++; i--; } } } char findduplicate(char *s) { char c='*'; int i,j; for(i=0;s[i];i++) { if(!(s[i]==c)) { for(j=i+1;s[j];j++) { if(s[i]==s[j]) s[j]=c; } } } return c; } int main() { char s[1000],c; printf("Enter the string : "); gets(s); c=findduplicate(s); deleteduplicate(s,c); printf("string after removing all duplicates:"); printf("%s",s); return 0; } |
Output:
1 2 |
Enter the string: aabbccddeeffgghhiijjkkllmmnnooppqq string after removing all duplicates:abcdefghijklmnopq |