2015-10-26 36 views
0

我正在嘗試寫一段C代碼,它接受一個句子並返回該句子中的所有迴文,每個句子都換行。例如,一句「我喜歡比賽公民的賽車」將返回: 公民 賽車在句子中找到palindromes

我試着用一些調試軟件(LLDB,因爲我是Mac用戶),但是發現它有點混亂。下面的代碼是我寫的。它正在返回一個分段錯誤,並且在我的程序中無法識別它。

int is_palin(char c[], int length) 
{ 
    int front = 0; 
    int back = length - 1; /* account for length starting at 0 */ 
    if (length % 2 == 0){ /* check for even palindromes */ 
      int middle = (length /2) -1 ; 
     while (front< middle + 1){ 
       if (c[front] != c[back]){ 
        return 0;} 
     front = front + 1; 
     back = back -1; 

     } 
    } 
    else {         /* check for odd palindromes */ 
      int middle = ((back - 2)/2) + 1; 
       while (front != middle){ 
        if (c[front] != c[back]){ 
          return 0;} 

       front = front + 1; 
       back = back -1;} 
      } 
       return 1; 
} 

int is_delimiting_char(char ch) 
{ 
if(ch == ' ')  //White space 
    return 1; 
else if(ch == ',') //Comma 
    return 1; 
else if(ch == '.') //Period 
    return 1; 
else if(ch == '!') //Exclamation 
    return 1; 
else if(ch == '?') //Question mark 
    return 1; 
else if(ch == '_') //Underscore 
    return 1; 
else if(ch == '-') //Hyphen 
    return 1; 
else if(ch == '(') //Opening parentheses 
    return 1; 
else if(ch == ')') //Closing parentheses 
    return 1; 
else if(ch == '\n') //Newline (the input ends with it) 
    return 1; 
else 
    return 0; 
} 


///////////////////////////////////////////// 

//--------------------------------------------------------------------------- 
// MAIN function 
//--------------------------------------------------------------------------- 


int main (int argc, char** argv) { 

    char input_sentence[100]; 
    int i=0; 
    char current_char; 
    int delimiting_char; 
    char word[20]; 
    int word_length; 
    int have_palindrome = 0; 


    /////////////////////////////////////////////  



    ///////////////////////////////////////////// 

    /* Infinite loop 
    * Asks for input sentence and prints the palindromes in it 
    * Terminated by user (e.g. CTRL+C) 
    */ 

while(1) { 

i=0;  

print_char('\n'); 

print_string("input: "); 

/* Read the input sentence. 
    * It is just a sequence of character terminated by a new line (\n) character. 
    */ 

do {   
    current_char=read_char(); 
    input_sentence[i]=current_char; 
    i++; 
} while (current_char != '\n'); 

///////////////////////////////////////////// 

print_string("output:\n"); 
int char_index = 0;   

for(int k=0; k<i; k++) { 
palin = 1; 
    current_char = input_sentence[k]; 
    delimiting_char = is_delimiting_char(current_char); 


if(delimiting_char) { 
    if (char_index > 0) {  //Avoids printing a blank line in case of consecutive delimiting characters. 
    word[char_index++] = '\n'; //Puts an newline character so the next word in printed in a new line. 
     word_length = word_length + 1; 
     if (is_palin(word, word_length) && word_length > 1){ 
     have_palindrome = 1; 
     for(int j=0; j<char_index; j++) {  
     print_char(word[j]); 
    } 
     word_length = 0; 
     char_index = 0; 
     } 
} } 
else { 
    word[char_index++] = current_char; 
    word_length = word_length + 1; 
} 

        } 


if (have_palindrome == 0){ 
    print_string("Sorry! No palindromes found!"); } 
} 

return 0; 

} 

也想知道如果任何人有良好的視頻或learnign如何使用LLDB網站,當一個人從未使用過這種事情。謝謝!

+0

「分而治之」是追蹤故障的一種方法。類似於減半,應該不需要很長時間來追蹤故障位置。至於'is_delimiting_char()'你不能使用'if(!isaplha(current_char))'嗎?甚至是'isspace()'。 –

回答

0

錯在這裏有幾件事情:

  • word_length在第一次使用時未初始化的,所以像word_length = word_length + 1導致未定義行爲的聲明。實際上,您有兩個不同的變量,char_indexword_length,它們應始終具有相同的值。而不是通過麻煩保持同步,只使用一個變量。
  • 只有在找到迴文時,纔會將char_indexword_length重置爲零。當然,如果在每個單詞之後,你應該重置。
  • palin = 1;可能是從舊代碼剩餘。每行後您還應該重置have_palindrome。一般來說,在定義變量時應該多加小心。
  • 通過在您的單詞中添加換行符可以使打印更容易一些,但是您將永遠找不到迴文,因爲檢查迴文時會考慮最後一個換行符。
  • 您的代碼爲read_char,可能是getchar的別名,需要檢查輸入的結尾。
  • 你不需要區分偶數和奇數大小的迴文。只要使條件,front < back和完成它。奇數大小的迴文中間字符無關緊要。 (這不是錯誤,你的代碼只是不必要的複雜。)