[英]C Program to Check for Palindrome String
我编写了两个示例程序来检查回文字符串。 但是在我得到的输出中,它不是回文数。 我缺少什么?
我严格假设代码以某种方式执行我的 if 语句并将标志放入 1。可能是因为长度计算。 有人有更好的主意吗?
#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;
}
和
#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;
}
您在第一个程序中使用的算法涉及将每个字母与每个其他字母进行比较,这无助于确定数字是否为回文,并且似乎无法修复。
然而,第二种方法的问题是您总是将name[i]
与name[length]
。 而是将其更改为length-i-1
。 这将从length-1
开始比较,并在每次下一次迭代时将字符的长度减 1:
for(i = 0;i <= length / 2;i++)
{
if(name[i] != name[length-i-1])
{
flag=1;
break;
}
}
gets()
和缓冲区溢出不要使用gets
。 此方法易受缓冲区溢出的影响。 如果您输入的字符串长度超过 100 个字符,则会导致未定义的行为。 使用fgets
代替确定性行为:
fgets(name, sizeof(name), stdin);
这需要缓冲区的大小,并且最多只能读取sizeof(name)
字符。
理想情况下,您应该考虑包装逻辑以检查字符串是否为函数中的回文:
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;
}
试试这个
#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;
}
第一段代码的问题是您比较的次数超过了要求,将其与length-i-1
进行比较。
第二个代码的主要问题是您仅将其与单词的最后一个字母进行比较。
希望你明白你的错误
你的两次尝试都有很多错误。 我强烈建议使用调试器来调查您的代码如何工作(或不工作)。
您的第一次尝试执行length
2 (不正确)比较,显然只需要length / 2
比较。 第二个执行length / 2
比较,但比较不正确:
name[i] != name[length-1] ;
应该:
name[i] != name[length - i - 1] ;
最后,当您知道它们不是回文时(在第一次不匹配时)可以终止比较时,您将进行彻底迭代。
可能还有其他错误 - 老实说,我没有比明显的更进一步,因为有更好的解决方案。
建议:
#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 ;
}
正在使用:
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;
}
输出:
xyyx : Is palindrome
xyayx : Is palindrome
xyxy : Is not palindrome
xyaxy : Is not palindrome
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.