2016-11-30 10 views
1

私のCプログラムにはいくつかの苦労があります! 文字列がパリンドロームかどうかチェックする必要があります。アルファベット以外の文字には注意を払うべきではないので、プログラムはこれを回文として認識すべきです! "彼は悪魔として住んでいましたか?"C文字列がパリンドロムかどうかを確認するプログラム

#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 PALINDROMEを私はこれまでに得たものです ! 1であっても。

+1

のように見えるかもしれないあなたも、基本的なのprintfデバッグをしようとしましたか? 'fgets'が文字列の最後に' \ n'を残していると考えましたか? –

+1

'right == 40'が初期値であった場合、' sentence [right] 'は無効です。 – timrau

+2

キャラクタを小文字または大文字に変換する必要があります。 'H!= h'。 –

答えて

1

私はあなたのプログラムにいくつかの変更を行いました。最初に配列のインデックスを破らず、次に未定義の値にアクセスする代わりに文字列の長さを使用し、3番目に同じ文字をチェックします。

#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

Trueですが、39バイトしか保持できないので' sentence'を読み込み中にもう少し早くクラッシュするかもしれませんが、 fgets(文、40、標準); ' – Gerhardh

+0

これは正しいです。彼は文のサイズを変更するか、またはfgetsを更新する必要があります。 –

1

あなたは、入力された文が回文であるかどうかをチェックする別の関数を書くことができます。その後、あなたのコードのための

として、これらのステートメント

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

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

未定義の動作にリード配列の文章は、あなたが40個の文字を入力しようとしている間だけ39の要素を持っているので。また、入力された文字列は40文字以上のkessを持つことができます。文字列の長さを決定する必要があります。

ここには、対応する関数の記述方法を示すデモンストレーションプログラムがあります。

#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): 
関連する問題