2016-11-30 32 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):