2016-01-22 83 views
0

我想创建一个程序来检查一个给定的数组/字符串是否是回文并且它不工作。该程序只在每个给定的阵列上打印“0”,即使在回文中也是如此。程序检查一个数组是否是回文

int main() 
{ 

    char string[100]= {0}; 
    char stringReverse[100]= {0}; 

    int temp = 0; 
    int firstLetter = 0; 
    int lastLetter = 0; 

    printf("Please enter a word or a sentence: "); 
    fgets(string, 100, stdin); 

    strcpy(stringReverse , string); // This function copies the scanned array to a new array called "stringReverse" 

    firstLetter = 0; 
    lastLetter = strlen(string) - 1; //because in array, the last cell is NULL 

    // This while reverses the array and insert it to a new array called "stringReverse" 
    while(firstLetter < lastLetter) 
    { 
     temp = stringReverse[firstLetter]; 
     stringReverse[firstLetter] = stringReverse[lastLetter]; 

     stringReverse[lastLetter] = temp; 

     firstLetter++; 
     lastLetter--; 
    } 

    printf("%s %s", stringReverse, string); 

    if (strcmp(stringReverse , string) == 0) 
    { 
     printf("1"); 
    } 
    else 
    { 
     printf("0"); 
    } 
} 
+4

对于这样一个简单的任务代码太多。 –

+0

我已经发布了类似问题的答案[这里](http://stackoverflow.com/a/33806921/4487286),你可以很容易地适应你的需求 – milevyo

回答

4

比方说,我们实现一个简单有趣的事情是

int check_palindrome (const char *s) { 
    int i,j; 
    for (i=0,j=strlen(s)-1 ; i<j ; ++i, --j) { 
     if (s[i] != s[j]) return 0; // Not palindrome 
    } 
    return 1; //Palindrome 
} 

我觉得这是更简单;)

对于贴有问题的代码: 注意与fgets的( )。它停在第一个'\ n'或EOF并保持'\ n'字符。

所以,如果你给雷达为前,结果字符串将“雷达\ n”,不与“\ nradar”

+0

没有比这更简单的方法来做到这一点。这就是你需要的一切。不要忘记写一个函数来检查前导/尾随空格,你应该没问题。如果遇到问题,C实际上有一个名为'isspace()'的函数,它接受一个字符(技术上它接受一个int,但只是给它一个字符),并返回一个非零值,如果它是一个空白字符\ n,\ t,\ v或空格。否则返回0。 – James

1

fgets增加了一个 '\ n' 匹配最后。 因此,如果用户输入“aba”,string包含“aba \ n”。 reverseString包含“\ naba”。

所以它不匹配。

在与fgets之后,将其复制到reverseString之前添加此代码

int l = strlen(string) - 1; 
string[l] = 0; 

这将去除出“\ N”末。除此之外,您可以在不需要第二个缓冲区或strcpystrlen调用的情况下完成整个程序。

+0

细节:'fgets()'不_add_''\ n''。它很简单,像所有其他输入字符一样保存。如果用户输入只有3个字符:'“aba”',没有键,然后'stdin'被关闭,输入只会是'a'',''b'',''c''。 'fgets()'不会添加终止空字符。要安全删除潜在的''\ n'',请考虑http://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/27729970#27729970 – chux

1

你在你的代码的几个问题:

  • 首先你忘了上次大括号};
  • 然后您忘记删除尾部\n(或者也可能在Windows下\r)在string;
  • 您不需要将字符串还原为新字符串;一通检查就足够了:

这里是一个工作代码:

#include <stdio.h> 
#include <string.h> 
int main() 
{ 

    char string[100]= {0}; 

    int temp = 0; 
    int firstLetter = 0; 
    int lastLetter = 0; 

    printf("Please enter a word or a sentence: "); 
    fgets(string, 100, stdin); 

    firstLetter = 0; 
    lastLetter = strlen(string) - 1; //because in array, the last cell is NULL 
    while ((string[lastLetter]=='\n')||(string[lastLetter]=='\r')) { 
     lastLetter--; 
    } 

    // This while reverses the array and insert it to a new array called "stringReverse" 
    temp = 1; 
    while(firstLetter < lastLetter) 
    { 
     if (string[firstLetter] != string[lastLetter]) { 
      temp = 0; 
      break; 
     } 

     firstLetter++; 
     lastLetter--; 
    } 

    if (temp) 
    { 
     printf("1"); 
    } 
    else 
    { 
     printf("0"); 
    } 
} 
+0

注意:'fgets(string, ...); int lastLetter = strlen(string) - 1; while((string [lastLetter] ...')是一种黑客攻击,'string'的第一个字符为''\ 0'',然后'string [-1]'为UB,这很容易。 http://chackoverflow.com/q/2693776/2410359 – chux

+0

@chux是的,但这只是对原始海报帖子的改编;我只是改变了有问题的帖子,当然其他的东西也可以改进。 –

2

问题:

比方说,你输入的字符串RACECAR输入您的程序,并按下输入时,这会在您的缓冲流中放入一个换行符或一个'\n',并且这也将作为您的字符串的一部分被fgets读取,因此您的程序将有效地结束检查RACECAR\n是回文,它不是

解决方案:

后初始化lastLetterstrlen(string) - 1检查,如果在字符串中的最后一个字符(或在lastLetter指数的字符是换行符(\n)如果是这样,减一lastLetter让你的程序检查,如果你的字符串(RACECAR)的其余部分是回文。

lastLetter = strlen(string) - 1; //because in array, the last cell is NULL 

// Add these 2 lines to your code 
// Checks if the last character of the string read by fgets is newline 
if (string[lastLetter] == '\n') 
    lastLetter--; 
0

您可以通过这个朴也做到这一点。

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

int main() 
{ 
    char string[10], revString[10]; 
    printf("Enter string for reversing it...\n"); 
    scanf("%s", string); 

    int stringLength = strlen(string); 

    for(int i = 0; string[i] != '\0'; i++, stringLength--) 
    { 
    revString[i] = string[stringLength - 1]; 
    } 

    if(strcmp(string, revString) == 0) 
     printf("Given string is pelindrom\n"); 
    else 
     printf("Given string is not pelindrom\n"); 
} 
+0

建议'scanf(“%s”,tmp);'和编码'gets(tmp)'一样好,不推荐。 – chux

相关问题