1

我正在尝试确定一个短语是否是回文(从左到右相同的单词),但我无法使其工作。怎么了?,我不能使用指针或递归或字符串类型变量

#include <stdio.h>

#include <string.h>

int main()

{

 int i,j = 0,length;
 char space = ' ';
 char phrase [80],phrase2[80],phrase3[80];

 printf("Give me the phrase: ");
 gets(phrase);
 length = strlen(phrase);

 for(i =0; i <= length - 1; i++)
 {
  if(phrase[i] != space)    //Makes the phrase without spaces
  {
   phrase2[i] = phrase[i];
   j++;
  }
 }

 for(i = length -1; i >= 0;i--)
 {
  if(phrase[i] != space)    //Makes the phrase backwards an without spaces
  {
   phrase3[j] = phrase[i];
   j++;
  }
 }

 length = strlen(phrase2);

 for(i =0; i <= length -1;i++)      //Compare the phrases to know if they are the same
 {
  if(phrase2[i] != phrase3[i])
  {
   printf("It's not a palindrome\n"); 
   return 0;
  }
 }
 printf("It's a palindrome\n");
 return 0; 
}
4

5 回答 5

2

Try this:

 for(i =0, j=0; i <= length - 1; i++)
 {
  if(phrase[i] != space)    //Makes the phrase without spaces
  {
   phrase2[j] = phrase[i];
   j++;
  } 
 }

 for(i = length -1, j = 0; i >= 0;i--)
 {
  if(phrase[i] != space)    //Makes the phrase backwards an without spaces
  {
   phrase3[j] = phrase[i];
   j++;
  }
 }

 length = j;

Update

In response to Praetorian's post here's the code to do it without copying the string.

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

int main()
{
  int i, j, length;
  char space = ' ';
  char phrase[80];

  printf("Give me the phrase: ");
  gets(phrase);
  length      = strlen(phrase);

  for( i = 0, j = length - 1; i < j; i++, j-- ) {
    while (phrase[i] == space) i++;
    while (phrase[j] == space) j--;
    if( phrase[i] != phrase[j] ) {
      printf("It's not a palindrome\n");
      return 0;
    }
  }

  printf("It's a palindrome\n");
  return 0; 
}
于 2010-10-14T01:11:46.117 回答
1

其他人已经回答了您的问题,但我发布此代码以表明没有必要制作phrase3副本来保存反转的字符串。

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

int main()
{

  int i, j, length, halfLength;
  char space = ' ';
  char phrase1[80], phrase2[80];

  printf("Give me the phrase: ");
  gets(phrase1);
  length      = strlen(phrase1);

  for( i = 0, j = 0; i <= length; ++i ) {
    if( phrase1[i] != space ) {    //Makes the phrase1 without spaces
      phrase2[j++] = phrase1[i];
    }
  }

  length      = strlen(phrase2);
  halfLength  = length / 2;

  for( i = 0, j = length - 1; i < halfLength; ++i, --j ) {
    if( phrase2[i] != phrase2[j] ) {
      printf("It's not a palindrome\n");
      return 0;
    }
  }

  printf("It's a palindrome\n");
  return 0; 
}
于 2010-10-14T01:58:44.380 回答
1

在第二个循环之前,您要设置 j=0。它应该在那之后工作。

PS:如果你通过打印出你的三个字符串来调试,你会在几分钟内弄清楚。当你不知道出了什么问题时,在中间步骤打印出变量的值,这样你就知道你的问题出现在哪里以及它是什么。

于 2010-10-14T01:31:02.080 回答
0

这就是我想出的:

#include <stdio.h>
void main() {
char a[50],b[50];
int i=0,j,ele,test=0,x;
while((a[i]=getchar())!='\n') {
if(a[i]!=' ' && a[i]!=',') //do not read whitespaces and commas(for palindromes like "Ah, Satan sees Natasha")
i++;
}
a[i]='\0';
ele=strlen(a);
// Convert string to lower case (like reverse of Ava is avA and they're not equal)
for(i=0; i<ele; i++)
if(a[i]>='A'&&a[i]<='Z')
a[i] = a[i]+('a'-'A');
x = ele-1;
for(j=0; j<ele; j++) {
b[j] = a[x];
x--;
}
for(i=0; i<ele; i++)
if(a[i]==b[i])
test++;
if(test==ele)
printf("You entered a palindrome!");
else
printf("That's not a palindrome!");
}

可能不是回文的最佳方式,但我很自豪我自己做了这个花了我 1 小时:( 哈哈

于 2013-01-22T19:14:44.967 回答
-1

为什么不使用std::stack? 您将需要两个循环,每个循环都迭代输入字符串的长度。在第一个循环中,遍历输入字符串一次,将每个字符压入堆栈。在第二个循环中,从堆栈中弹出一个字符并将其与索引处的字符进行比较。如果在循环结束之前出现不匹配,则没有回文。这样做的好处是您不必担心偶数/奇数长度的角盒。它会起作用的。

(如果您愿意,可以使用一个堆栈(LIFO)和一个队列(FIFO),但这并不会显着改变算法)。

这是实现:

bool palindrome(const char *s)
{
    std::stack<char> p; // be sure to #include <stack>

    for(int i = 0; s[i] != 0; i++)
        p.push(s[i]);

    for(int i = 0; s[i] != 0; i++)
    {
        if(p.top() != s[i])
            return false; // not a palindrome!

        p.pop();
    }    

    return true;
}

跳过空格作为练习留给读者;)

于 2013-01-22T19:37:34.650 回答