2016-11-30 100 views
1

我有一些與我的C程序掙扎! 它應該檢查一個字符串是否是迴文或不!它不應該關注非字母字符,所以程序應該認識到這是一個迴文。 「他住過魔鬼,呃?」 這就是我走到這一步:C程序來檢查字符串是否是Palindrome

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    char sentence[39]; 
    int left = 0; 
    int right = 40; 

    printf("Enter a message: "); 
    fgets(sentence, 40, stdin); 

    while(1) { 

     while(left < right && !(isalpha(sentence[left]))) 
      left++; 
     while(right > left && !(isalpha(sentence[right]))) 
      right--; 

     if(left >= right) 
      break; 

     else { 

      if(sentence[left] != sentence[right]) { 
       printf("Not a Palindrome"); 
       return 0; 
      } 

      left++; 
      right--; 
     } 
    } 

    printf("Palindrome"); 

    return 0; 
} 

它總是打印:NOT迴文! 即使它是一個。

+1

你嘗試連基本的printf調試?你認爲'fgets'在字符串的末尾留下了一個'\ n'嗎? –

+1

'句子[右]'當'right == 40'是初始值時無效。 – timrau

+2

您應該將字符轉換爲大寫或小寫。 'H!= h'。 –

回答

1

我已經對你的程序進行一些更改。首先不要破壞數組索引,接下來使用字符串長度而不是訪問未定義的值,第三次檢查相同的大小寫字母。

#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 

int main() 
{ 
    char sentence[200];        // provide plenty of room 
    int left = 0; 
    int right;          // do not assume the length 

    printf("Enter a message: "); 
    fgets(sentence, sizeof sentence, stdin);  // limit the input 
    right = strlen(sentence);      // now get the length 

    while(1) { 
     while(left < right && !(isalpha(sentence[left]))) 
      left++; 
     while(right > left && !(isalpha(sentence[right]))) 
      right--; 
     if(left >= right) 
      break; 
     else { 
      if(toupper(sentence[left]) != toupper(sentence[right])) { // get case the same 
       printf("Not a Palindrome\n"); 
       return 0; 
      } 
      left++; 
      right--; 
     } 
    } 

    printf("Palindrome\n"); 
    return 0; 
} 

程序會話:

 
Enter a message: He lived as a devil, eh? 
Palindrome 

Enter a message: palindrome 
Not a Palindrome 
0

你應該初始化權作爲字符串的結尾:

#include <string.h> 

// ... 

    right = strlen(sentence) - 1; 
+1

是真的,但它在閱讀'sentence'時可能會早一點崩潰,因爲它只能保存39個字節,但它使用' fgets(句子,40,stdin);' – Gerhardh

+0

這是正確的。他應該調整句子大小或更新fgets。 –

1

你可以寫一個單獨的函數,它檢查輸入的句子是否是一個迴文。

至於你的代碼,然後這些語句

char sentence[39]; 
int left = 0; 
int right = 40; 

printf("Enter a message: "); 
fgets(sentence, 40, stdin); 

導致不確定的行爲,因爲數組句話有,而你試圖輸入40個字符只有39元。此外,輸入的字符串可以包含超過40個字符。您需要確定字符串的長度。

這是一個演示程序,顯示如何寫入相應的功能。

#include <string.h> 
#include <ctype.h> 
#include <stdio.h> 

int is_palindrome(const char *s) 
{ 
    size_t n = strlen(s); 

    const char *first = s, *last = s + n; 

    if (n) 
    { 

     do 
     { 
      while (*first && !isalpha((unsigned char)*first)) ++first; 
      if (first != last) 
      { 
       while (!isalpha((unsigned char)*--last)); 
      } 
     } while (toupper((unsigned char)*first) == 
        toupper((unsigned char)*last) && 
        first != last && 
        ++first != last); 
    } 

    return first == last; 
} 

#define N 100 

int main() 
{ 
    while (1) 
    { 
     char s[N]; 

     printf("Enter a sentence (Enter - exit): "); 

     if (!fgets(s, sizeof(s), stdin) || s[0] == '\n') break; 

     printf("\nThe sentence is%s palindrome.\n\n", 
      is_palindrome(s) ? "" : " not"); 
    } 

    return 0; 
} 

它的輸出可能看起來像

Enter a sentence (Enter - exit): He lived as a devil, eh 

The sentence is palindrome. 

Enter a sentence (Enter - exit):