2012-03-12 95 views
0

我寫了下面的函數用於從string..For前刪除重複的字符:如果 海峽=「heeello; removeDuplicate(STR)C程序...顯示運行時錯誤

將返回直升機...但它顯示了運行時的一些錯誤。我增加了一些的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; 
} 
+0

你可以顯示你對這個函數的調用嗎? – MByD 2012-03-12 10:34:59

+0

removeDuplicate(「heee」); – 2012-03-12 10:37:12

+0

是的,這是呼叫問題... heee沒有\ 0:P ..是它的問題嗎? – 2012-03-12 10:39:30

回答

2

您傳遞字符串字面,你不能修改這個功能,而應該這樣做:

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

而且,請注意,下面幾行你必須給的printf(%c %d)內符,但你只傳遞一個參數(str[j]):

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

這可能會導致各種不良事情......

+0

實際上,我打印j也與...這是一個意外的錯誤...也是什麼問題的邏輯? – 2012-03-12 10:43:52

+0

是的,得到了​​實際的邏輯問題,並改變了while循環爲 while(str [j]!(str [j]!='\ 0') if(ch == str [j]) while(str [j]!='\ 0') str [j] = str [++ j];我 - ; 休息; } j ++; } – 2012-03-12 10:47:23

2

你應該糾正你的代碼如下:

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

第一個循環'j = i'是正確的。 – duedl0r 2012-03-12 10:49:52

+0

我已經改變了錯誤,併發布爲Binyamin Sharet的回答評論看到.. – 2012-03-12 14:18:53

1

我不認爲牛逼他的功能是做你想做的。刪除循環真的有鬼..你遞減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? 
} 

我會去一個更簡單的方法。創建一個大布爾數組。循環訪問字符串並存儲您是否已經遇到當前字符。如果不是,打印它。

1

檢查以下代碼:

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

我已校正代碼如下

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

請添加一些解釋.. – Shivaay 2013-08-15 14:16:35

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(); 
} 
0

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/