C语言中无指针和递归的回文

C语言中无指针和递归的回文,c,palindrome,C,Palindrome,我试图确定一个短语是否是回文(一个从左到右都相同的单词),但我无法让它工作。怎么了?我不能使用指针、递归或字符串类型变量 #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: "); get

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

#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; 
}
#包括
#包括
int main()
{
int i,j=0,长度;
字符空间=“”;
字符短语[80],短语2[80],短语3[80];
printf(“给我一个短语:”);
获取(短语);
长度=strlen(短语);
对于(i=0;i=0;i--)
{
if(短语[i]!=space)//使短语向后变为不带空格的
{
短语3[j]=短语[i];
j++;
}
}
长度=strlen(短语2);
对于(i=0;i试试这个:

 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;
(i=0,j=0;i=0;i--)的

{
if(短语[i]!=space)//使短语向后变为不带空格的
{
短语3[j]=短语[i];
j++;
}
}
长度=j;
更新

作为对Praetorian帖子的回应,这里有一段代码,可以在不复制字符串的情况下完成此操作

#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; 
}
#包括
#包括
int main()
{
int i,j,长度;
字符空间=“”;
字符短语[80];
printf(“给我一个短语:”);
获取(短语);
长度=strlen(短语);
对于(i=0,j=length-1;i
在第二个循环之前,您需要设置j=0。在这之后,它应该可以工作


PS:如果您通过打印三个字符串进行调试,您将在几分钟内解决问题。如果您不知道出现了什么问题,请在中间步骤打印变量的值,这样您就知道问题发生的位置和原因。

您的问题已经得到了其他人的回答,但我发布此代码是为了说明问题无需复制短语3来保存反向字符串

#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; 
}
#包括
#包括
int main()
{
int i,j,长度,半长度;
字符空间=“”;
char短语1[80],短语2[80];
printf(“给我一个短语:”);
获取(短语1);
长度=strlen(短语1);

对于(i=0,j=0;i这就是我想到的:

#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!");
}
#包括
void main(){
字符a[50],b[50];
int i=0,j,ele,test=0,x;
而((a[i]=getchar())!='\n'){
if(a[i]!='&&a[i]!=',')//不要读空格和逗号(对于回文,如“啊,撒旦看见娜塔莎”)
i++;
}
a[i]='\0';
ele=strlen(a);
//将字符串转换为小写(如Ava的反面是Ava,它们不相等)

对于(i=0;i='A'&&A[i]为什么不使用
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;
}
bool回文(const char*s)
{
std::stack p;//确保#包括
对于(int i=0;s[i]!=0;i++)
p、 推(s[i]);
对于(int i=0;s[i]!=0;i++)
{
如果(p.top()!=s[i])
return false;//不是回文!
p、 pop();
}    
返回true;
}

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

有错误消息吗?您的示例输入和输出是什么?我总是得到“这不是回文”信息,我输入的一个例子永远不会是奇数或偶数。短语2应该是Neverodoreven,短语3应该是没有空格的倒转单词,这是相同的Neverodoreven。我喜欢C作业,上面写着“你不能使用指针或递归”——因为这太容易了,或者可能不是一对有用的函数也可以在一个循环中完成,而无需复制字符串。在从每一端解析字符串时,只需检查空格并适当调整索引。