用于删除字符串中重复字符的C程序…显示运行时错误

我编写了以下函数来从字符串中删除重复的字符。例如:如果str =“heeello; removeDuplicate(str)

将返回helo …但它在运行时显示一些错误。我已经添加了一些printf()语句用于调试…有谁能告诉我这是什么问题?

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; } 

您正在传递一个字符串文字,您不能修改此function,而应该执行以下操作:

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

另请注意,在以下行中,您必须在printf( %c %d )中使用说明符,但是只传递一个参数( str[j] ):

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

这可能会导致各种不好的事情……

您应该按如下方式更正代码:

  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]; 

我认为这个function没有你想做的。 删除循环真的很可疑..你减少了i看起来错了..你增加j可能也是错的:

 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? } 

我会采取更简单的方法。 创建一个大的bool数组。 循环遍历字符串并存储您是否已经遇到当前字符。 如果没有,请打印出来。

检查以下代码:

 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) ); } 
 //removing the redundant characters in a string #include 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 

我已经更正了如下代码

 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; } 
 #include #include #include #include void main() { clrscr(); char *str; int count=0; cout<<"enter the string which have repetative characters"<>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(); } 

O(n)复杂性

 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; } 

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