Tuesday, September 30, 2008

How to remove duplicate characters in a string using C?

Hai friends,
one of my friend asked me to write a C program which should remove duplicate charaters and print the string in C.
That is the reason why i have deviated todays content with C Program.

Input : Apple
Output : Aple

Input :AABBCC
Output :
I have not runned this program in my machine as i am not having TurboC...
Vara prasaad could please check with this and update me whether am i correct.

C-Program

#include<stdio.h>
#include<string.h>
void main()
{
char str[20];finStr[20];
int strlength=0;int i=0;
int fnInd=0;
printf("Program to remove duplicate charaters from the string\n");
printf("Enter string\n");
scanf("%s",str);
strlength=strlen(str);
for(i=0;i<strlength;i++)
{
if(!isDuplicateAvailable(str,str[i],i))
{
finStr[fnInd]=str[i];
fnInd++;
}
}
printf("\n String after removing duplicates :\n%s",finStr);
}


bool isDuplicateAvailable(char[] argStr,char argChar,int argIndex)
{
for(int j=0;j<strlen(argStr);j++)
{
if(j!=argIndex && argStr[j]==argChar)
{
retrun true;
}
}
return false;
}

Cheers,
Srinivas

No comments: