0

I wrote the following function for removing duplicate characters from a string..For ex: if str = "heeello; removeDuplicate(str)

will return helo...But it shows some error on runtime .I have added some printf() statements for debugging...Can anyone tell me what the problem is ?

char* removeDuplicate(char str[])//remove duplicate characters from a string,so that each character in a string is not repeating { int i = 0,j; char ch; printf("\nstr is %s",str); while((ch = str[i++] )!= '\0') { j = i; printf("\n----ch = %c----",ch); while(str[j] != '\0') { printf("\n--------Checking whether %c = %c \n",str[j],ch); if(ch == str[j]) { printf("\n------------Yes"); while(str[j]!='\0') { printf("\nRemoving %c %d -- \n",str[j]); str[j] = str[++j]; --i; } break; } printf("\n------------No"); //printf("\njj"); j++; } } return str; } 
6
  • Can you show your call to this function? Commented Mar 12, 2012 at 10:34
  • removeDuplicate("heee"); Commented Mar 12, 2012 at 10:37
  • yes that was the problem with call...heee doesnot have \0 :P..Is it the problem? Commented Mar 12, 2012 at 10:39
  • 1
    Actually, it does have \0 "heee" is just like {'h', 'e', 'e', 'e', '\0'} Commented Mar 12, 2012 at 10:44
  • Your code contains (at least) two dangerous styles, which probably cause this function to behave different than you have in mind: Dont use a variable and ++ it in the same expression: str[j] = str[++j];. The C specification doesn't specify how the compiler should behave in this case (see c-faq.com/expr/evalorder1.html). Also doing i-- is not a very wise thing to do in such constructions. In this case i will become negative in a lot of cases, and str[-1] is clearly not valid here. Commented Mar 12, 2012 at 10:52

8 Answers 8

2

You are passing a string literal, which you are not allowed to modify to this function, instead you should do:

char myStr[] = "heee"; removeDuplicate(myStr); 

Also, please note that in the following lines your have to specifiers inside the printf (%c %d), but you pass only one argument (str[j]):

printf("\nRemoving %c %d -- \n",str[j]); 

This may cause all sorts of bad things...

Sign up to request clarification or add additional context in comments.

2 Comments

actually i printed j also with that ...it is an accidental mistake...also what is the problem in logic?
Yes got the actual logic problem and changed the while loop as while(str[j] != '\0') { if(ch == str[j]) { while(str[j]!='\0') str[j] = str[++j]; i--; break; } j++; }
2

You should correct your code as follows:

 In first while loop: j = i+1; In third while loop: i--; // is not required Remove that unwanted specifier form printf("Removing %d %d:",str[j]) Doing incorrectly : str[j] = str[++j] // you are increasing j before assigning str[j] = str[j++] // correct way to do.But it is compiler dependent i guess Better to use: t = j; str[t] = str[++j]; 

1 Comment

I have changed the bug and posted as comment for Binyamin Sharet 's answer see that..
1

I don't think this function does what you want. The remove loop is really fishy.. you decrement i which looks wrong.. and you increment j which is probably also wrong:

while(str[j]!='\0') { printf("\nRemoving %c %d -- \n",str[j]); str[j] = str[++j]; // now the new character is at location j, but since // you incremented j you can't access it anymore --i; // why is i dependent on the remove stuff? } 

I would go for a simpler approach. Create a large bool array. Loop through your string and store whether you already encountered the current character or not. If not, print it.

Comments

1

Check the following code :

char* removeDuplicate(char str[])//remove duplicate characters from a string,so that each character in a string is not repeating { int i = 0,j; char ch; int repIndex=0; int temp=0; printf("\nstr is %s",str); while((ch = str[i++] )!= '\0') { j = i; printf("\n----ch = %c----",ch); while(str[j] != '\0') { printf("\n--------Checking whether %c = %c \n",str[j],ch); repIndex = j; if(ch == str[repIndex]) { printf("\n------------Yes"); while(str[repIndex]!='\0') { printf("\nRemoving %c %d \n",str[j]); temp = repIndex; str[temp] = str[++repIndex]; } } else { j++; } } } return str; } int main ( int argc, char ** argv) { char myStr[]="asdfhelllasdfloofdoeohz"; printf ("OUtput is : %s \n", removeDuplicate(myStr) ); } 

Comments

1
//removing the redundant characters in a string #include<stdio.h> int main() { int i=0,j,arr[26]={},temp; //array for hashing char s[10],arr1[10],*p; //array 4 storing d output string printf("Enter the string\n"); scanf("%s",s); p=s; while(*p!='\0') { temp=((*p)>92)?(*p)-'a':(*p)-'A'; //asuming lowr and upr letters are same if(arr[temp]==0) //if it is not hashed ie if that char is not repeated { arr1[i]=temp+'a'; //return the string in lowecase arr[temp]=1; //storing value so that this character sd not be placed again i++; } p++; //else ignore the alphabet } for(j=0;j<i;j++) { printf("%c",arr1[j]); //print the string stored in arr1 } return 0; } 

Comments

0

I have corrected the code as follows

char* removeDuplicate(char str[])//remove duplicate characters from a string,so that each character in a string is not repeating { int i = 0,j; char ch; while((ch = str[i++] )!= '\0') { j = i; while(str[j] != '\0') { if(ch == str[j]) { while(str[j]!='\0') str[j] = str[++j]; i--; break; } j++; } } return str; } 

Comments

0
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { clrscr(); char *str; int count=0; cout<<"enter the string which have repetative characters"<<endl; cin>>str; char *str2; int m=0; for(int i=0;i<=strlen(str);i++) { char ch=str[i]; if(i==0) { str2[m]=str[i]; m++; } for(int j=0;j<=strlen(str2);j++) { if(ch==str2[j]) count++; } if(count==0) { str2[m]=str[i]; m++; } count=0; if(i==strlen(str)) str2[m]='\0'; } puts(str2); getch(); } 

Comments

0

O(n) complexity

char *removeDuplicates(char *str){ int hash[256] = {0}; int currentIndex = 0; int lastUniqueIndex = 0; while(*(str+currentIndex)){ char temp = *(str+currentIndex); if(0 == hash[temp]){ hash[temp] = 1; *(str+lastUniqueIndex) = temp; lastUniqueIndex++; } currentIndex++; } *(str+lastUniqueIndex) = '\0'; return str; } 

Refer: http://www.geeksforgeeks.org/remove-all-duplicates-from-the-input-string/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.