C program to remove all duplicate characters in a string; Through this tutorial, we will learn how to remove all duplicate characters in a string using for loop, while loop and function in c programs.
Programs and Algorithm to Remove All Duplicate Characters From String in C
Algorithm and programs to remove all duplicate characters in a string using for loop, while loop and function in c:
- Algorithm to Remove All Duplicate Characters in a String
- C Program to Remove All Duplicate Characters in a String using For Loop
- C Program to Remove All Duplicate Characters in a String using While Loop
- C Program to Remove All Duplicate Characters in a String using Function
Algorithm to Remove All Duplicate Characters in a String
Use the following algorithm to write a program to remove the duplicate character from a string; as follows:
- Start program.
- Take input string from user, store it in some variable.
- Find and remove all repeated characters of the given string.
- Print result
- End program
C Program to Remove All Duplicate Characters in a String using For Loop
/* C Program to Remove All Duplicate Character in a String */ #include <stdio.h> #include <string.h> int main() { char str[100]; int i, j, k; printf("\n Please Enter any String : "); gets(str); for(i = 0; i < strlen(str); i++) { for(j = i + 1; str[j] != '\0'; j++) { if(str[j] == str[i]) { for(k = j; str[k] != '\0'; k++) { str[k] = str[k + 1]; } } } } printf("\n The Final String after Removing All Duplicates = %s ", str); return 0; }
The output of the above c program; as follows:
The Final String after Removing All Duplicates = welcom t prgaminus
C Program to Remove All Duplicate Characters in a String using While Loop
/* C Program to Remove All Duplicate Character in a String */ #include <stdio.h> #include <string.h> int main() { char str[100]; int i, j, k; i = 0; printf("\n Please Enter any String : "); gets(str); while(i < strlen(str)) { j = i + 1; while(str[j] != '\0') { if(str[j] == str[i]) { k = j; while(str[k] != '\0') { str[k] = str[k + 1]; k++; } } j++; } i++; } printf("\n The Final String after Removing All Duplicates = %s ", str); return 0; }
The output of the above c program; as follows:
Please Enter any String : c programmer The Final String after Removing All Duplicates = c progame
C Program to Remove All Duplicate Characters in a String using Function
/* C Program to Remove All Duplicate Character in a String */ #include <stdio.h> #include <string.h> void Remove_AllOccurrence(char *str); int main() { char str[100]; printf("\n Please Enter any String : "); gets(str); Remove_AllOccurrence(str); printf("\n The Final String after Removing All Duplicates = %s ", str); return 0; } void Remove_AllOccurrence(char *str) { int i, j, k; for(i = 0; i < strlen(str); i++) { for(j = i + 1; str[j] != '\0'; j++) { if(str[j] == str[i]) { for(k = j; str[k] != '\0'; k++) { str[k] = str[k + 1]; } } } } }
The output of the above c program; as follows:
Please Enter any String : c programmer The Final String after Removing All Duplicates = c progame