C Program to Remove All Duplicate Characters in a String

In this tutorial, i am going to show you how to remove all duplicate characters in a string with the help of for loop, while loop and function in c programs.

All C Programs and Algorithm to Remove All Duplicate Characters From String

  • 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

Follow the below given algorithm to write a program to remove the duplicate character from a string; as follows:

  1. Start program.
  2. Take input string from user, store it in some variable.
  3. Find and remove all repeated characters of the given string.
  4. Print result
  5. 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 result 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 result 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 result of the above c program; as follows:

Please Enter any String :  c programmer
The Final String after Removing All Duplicates = c progame

More C Programming Tutorials

Leave a Comment