C 中的回文,没有指针和递归
我试图确定一个短语是否是回文(从左到右相同的单词),但我无法使其工作。怎么了?我不能使用指针、递归或字符串类型变量
#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;
}
I'm trying to determine if a phrase is a palindrome (a word that is the same from left to rigth) or not but i can't make it work. What's wrong?, i can't use pointers or recursion or string type variables
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
更新
作为对 Praetorian 帖子的回应,这里是无需复制字符串即可执行此操作的代码。
Try this:
Update
In response to Praetorian's post here's the code to do it without copying the string.
在第二个循环之前,您要设置 j=0。之后应该可以工作。
PS:如果您通过打印出三个字符串来进行调试,那么您只需几分钟就可以弄清楚。当您不知道出了什么问题时,请在中间步骤中打印出变量的值,这样您就知道问题发生在哪里以及问题是什么。
Before the 2nd loop you want to set j=0. It should work after that.
PS: If you debugged by printing out your three strings, you would've figured it out in a matter of minutes. When you don't know what goes wrong, print out the values of variables at intermediate steps, so you know where your problem occurs and what it is.
其他人已经回答了您的问题,但我发布此代码是为了表明没有必要制作
phrase3
副本来保存反转的字符串。Your question has already been answered by others but I'm posting this code to show that it is not necessary to make the
phrase3
copy to hold the reversed string.这就是我想出的:
可能不是回文的最佳方法,但我很自豪我自己花了 1 小时做了这个:( 哈哈
This is what I came up with:
Probably not the best way for palindromes, but I'm proud I made this on my own took me 1 hour :( lol
为什么不使用 std::stack ?您将需要两个循环,每个循环迭代输入字符串的长度。在第一个循环中,遍历输入字符串一次,将每个字符推入堆栈。在第二个循环中,从堆栈中弹出一个字符并将其与索引处的字符进行比较。如果在循环结束之前出现不匹配,则没有回文。这样做的好处是您不必担心偶数/奇数长度的极端情况。它会起作用的。
(如果您愿意,可以使用一个堆栈 (LIFO) 和一个队列 (FIFO),但这不会对算法产生实质性改变)。
这是实现:
跳过空格留给读者作为练习;)
Why not use a
std::stack
? You will need two loops, each iterating the length of the input string. In the first loop, go through the input string once, pushing each character ont the stack. In the second loop, pop a character off the stack and compare it with the character at the index. If you get a mismatch before the loop ends, you don't have a palindrome. The nice thing with this is that you don't have to worry about the even/odd length corner-case. It will just work.(If you are so inclined, you can use one stack (LIFO) and one queue (FIFO) but that doesn't substantially change the algorithm).
Here's the implementation:
Skipping spaces is left as an exercise to the reader ;)