[英]C Program to Check for Palindrome String
I wrote two sample programs to check for a palindrome string.我编写了两个示例程序来检查回文字符串。 But in both I am getting output like, its not a palindrome number.但是在我得到的输出中,它不是回文数。 What I am missing?我缺少什么?
I strictly assume somehow code is executing my if statement and put flag in to 1. May be because of that length calculation.我严格假设代码以某种方式执行我的 if 语句并将标志放入 1。可能是因为长度计算。 Anyone has a better idea?有人有更好的主意吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int main(void) {
setbuf(stdout,NULL);
char name[100];
int i,length,flag=0,k;
printf("Enter your name");
/*scanf("%s",name);*/
gets(name);
length=strlen(name);
for(i=0;i<=length-1;i++)
{
for(k=length-1;k>=0;k--)
{
if(name[i]!=name[k])
{
flag=1;
break;
}
}
}
if(flag==0)
{
printf("Give word is a palindrome");
}
if(flag==1)
{
printf("This is NOT a palindrome word");
}
return 0;
}
and和
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int main(void) {
setbuf(stdout,NULL);
char name[100];
int i,length,flag=0;
printf("Enter your name");
/*scanf("%s",name);*/
gets(name);
length=strlen(name);
for(i=0;i<=length/2;i++)
{
if(name[i]!=name[length-1])
{
flag=1;
}
}
if(flag==0)
{
printf("Give word is a palindrome");
}
if(flag==1)
{
printf("This is NOT a palindrome word");
}
return 0;
}
The algorithm you are using in the first program involves comparing each letter to every other letter which does not help in determining if the number is a palindrome and it does not seem fixable.您在第一个程序中使用的算法涉及将每个字母与每个其他字母进行比较,这无助于确定数字是否为回文,并且似乎无法修复。
The problem with the second approach, however, is you are always comparing name[i]
to name[length]
.然而,第二种方法的问题是您总是将name[i]
与name[length]
。 Instead change it to length-i-1
.而是将其更改为length-i-1
。 This will start comparing from length-1
and decrement the length of the character by 1 for every next iteration:这将从length-1
开始比较,并在每次下一次迭代时将字符的长度减 1:
for(i = 0;i <= length / 2;i++)
{
if(name[i] != name[length-i-1])
{
flag=1;
break;
}
}
gets()
and buffer overflow gets()
和缓冲区溢出Do not use gets
.不要使用gets
。 This method is susceptible to a buffer overflow.此方法易受缓冲区溢出的影响。 If you enter a string longer than 100 characters, it will result in undefined behavior.如果您输入的字符串长度超过 100 个字符,则会导致未定义的行为。 Use fgets
instead for deterministic behavior:使用fgets
代替确定性行为:
fgets(name, sizeof(name), stdin);
This takes in the size of the buffer and only reads up to sizeof(name)
characters.这需要缓冲区的大小,并且最多只能读取sizeof(name)
字符。
Ideally, you should consider wrapping the logic to check if the string is a palindrome in a function:理想情况下,您应该考虑包装逻辑以检查字符串是否为函数中的回文:
int is_palindrome(char*);
int main(void)
{
char name[100];
setbuf(stdout,NULL);
printf("Enter your name");
fgets(name, sizeof(name), stdin);
if(is_palindrome(name))
{
printf("The given word is a palindrome");
}
else
{
printf("This is NOT a palindrome word");
}
return 0;
}
int is_palindrome(char* name)
{
int length = strlen(name);
int flag = 0, i;
for(i = 0;i <= length / 2; i++)
{
if(name[i]!=name[length-i-1])
{
return 0;
}
}
return 1;
}
Try this试试这个
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char text[100];
int begin, middle, end, length = 0;
printf("enter the name: ");
scanf("%s",text);
while ( text[length] != '\0' ){
length++;}
end = length - 1;
middle = length/2;
for ( begin = 0 ; begin < middle ; begin++ ) {
if ( text[begin] != text[end] ) {
printf("Not a palindrome.\n");
break;
}
end--;
}
if( begin == middle )
printf("Palindrome.\n");
return 0;
}
The problem with the first piece of code is you are comparing it more than required, compare it with length-i-1
.第一段代码的问题是您比较的次数超过了要求,将其与length-i-1
进行比较。
The main problem with the second code is you are comparing it with only the last letter of a word.第二个代码的主要问题是您仅将其与单词的最后一个字母进行比较。
Hope you understood your mistake希望你明白你的错误
There is plenty wrong with both your attempts.你的两次尝试都有很多错误。 I strongly suggest using a debugger to investigate how your code works (or doesn't).我强烈建议使用调试器来调查您的代码如何工作(或不工作)。
Your first attempt performs length
2 (incorrect) comparisons, when clearly only length / 2
comparisons are required.您的第一次尝试执行length
2 (不正确)比较,显然只需要length / 2
比较。 The second performs length / 2
comparisons but the comparison is incorrect:第二个执行length / 2
比较,但比较不正确:
name[i] != name[length-1] ;
should be:应该:
name[i] != name[length - i - 1] ;
Finally you iterate exhaustively when you could terminate the comparison as soon as you know they are not palindromic (on first mismatch).最后,当您知道它们不是回文时(在第一次不匹配时)可以终止比较时,您将进行彻底迭代。
There may be other errors - to be honest I did not look further than the obvious, because there is a better solution.可能还有其他错误 - 老实说,我没有比明显的更进一步,因为有更好的解决方案。
Suggest:建议:
#include <stdbool.h>
#include <string.h>
bool isPalindrome( const char* str )
{
bool is_palindrome = true ;
size_t rev = strlen( str ) - 1 ;
size_t fwd = 0 ;
while( is_palindrome && fwd < rev )
{
is_palindrome = (str[fwd] == str[rev]) ;
fwd++ ;
rev-- ;
}
return is_palindrome ;
}
In use:正在使用:
int main()
{
const char* test[] = { "xyyx", "xyayx", "xyxy", "xyaxy" } ;
for( size_t t = 0; t < sizeof(test)/sizeof(*test); t++ )
{
printf("%s : %s palindrome\n", test[t],
isPalindrome( test[t] ) ? "Is" : "Is not" ) ;
}
return 0;
}
Output:输出:
xyyx : Is palindrome
xyayx : Is palindrome
xyxy : Is not palindrome
xyaxy : Is not palindrome
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.