检查字符串是否是回文 [英] check whether a string is palindrome
本文介绍了检查字符串是否是回文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include < stdio.h >
#include < stdlib.h >
int main()
{
char a [ 20 ];
int n;
printf( 输入字符串)的大小;
scanf( %d,& n);
printf( 输入字符串);
scanf( %s,& a [ 20 跨度>]);
for ( int i = 0 ; i<(n- 1 )/ 2; i ++)
{
if (a [i] == a [n- 1 -i])
{
printf( string is palindrome);
}
else
printf( string不是回文);
}
return 0 ;
}
当我运行这个程序时我得到堆栈异常...我的代码是错误的吗?...我的意思是流程错了?或者是由于使用scanf的某种缓冲区溢出导致的错误?任何人请帮助..
================
编辑:Matt T Heffron (代码从下面的评论中移出):
i修复了逻辑,检查了溢出问题,但仍未执行...有人可以解释...
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
int main()
{
char a [ 20 跨度>];
int n,c;
c = 0 ;
printf( 输入字符串)的大小;
scanf( %d,& n);
printf( 输入字符串);
fgets(a,n,stdin);
for ( int i = 0 ; i<(n- 1 )/ 2; i ++)
{
if (a [i] == a [n- 1 -i])
{
c = 0 跨度>;
}
其他
{
c = 1 ;
break ;
}
}
if (c == 0 )
printf( string is palindrome);
else
printf( string不是回文);
return 0 ;
}
解决方案
嗯...尝试更改此内容:
< pre lang =c ++> scanf( %s,& a [ 20 ]);对此:
scanf( < span class =code-string>%s,a);甚至:
scanf( %s,& a [ 0 ]);
#include < stdio.h >
#include < string.h >
int main(){
char string1 [ 20 < /跨度>];
int i,length;
int flag = 0 ;
printf( 输入字符串:);
scanf( %s,string1);
length = strlen(string1);
for (i = 0 ; i< length; i ++){
if (string1 [i]!= string1 [length-i- 1 ]){
flag = 1 ;
break ;
}
}
if (flag){
printf( %s不是回文,string1);
}
else {
printf( %s是回文,string1);
}
return 0 ;
}
或者借助strrev,strcmp这样的字符串函数:
#include < stdio.h >
# include < string.h >
int main()
{
char a [ 100 ],b [ 100 跨度>];
printf( 输入字符串以检查它是否为回文\\ n< /跨度>);
获得(a);
strcpy(b,a);
strrev(b);
if (strcmp(a,b)== 0 )
printf( 输入的字符串是palindrome。\ n);
else
printf( 输入的字符串不是palindrome.\
);
return 0 ;
}
-KR
除了我们指出的错误原始,你的代码在逻辑上有缺陷(它可以在实际检查整个字符串之前断言字符串是palindrome)。
尝试
#include < stdio.h >
#include < string.h >
#include < stdlib.h >
#define MAX_LEN 20
int main()
{
char a [MAX_LEN + 1];
printf( 输入字符串);
if (fgets(a,MAX_LEN + 1,stdin))
{
size_t len = strlen(a) - 1 ; // 摆脱'\ n'
size_t i;
printf( n =%d \ n,len);
for (i = 0 ; i< len / 2; i ++)
{
if (a [i]!= a [len- 1 -i]) break ;
}
if (i == len / 2)
printf( \ nstring是palindrome);
else
printf( \ nsnstring不是回文);
printf( \ n);
}
return 0 ;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
char a[20];
int n;
printf("enter the size of the string ");
scanf("%d",&n);
printf("enter the string ");
scanf("%s",&a[20]);
for(int i=0;i<(n-1)/2;i++)
{
if(a[i]==a[n-1-i])
{
printf("string is palindrome");
}
else
printf("string is not palindrome");
}
return 0;
}
when i am running this program i am getting stack exception...is my code wrong?...i mean is the process wrong? or is the error due to some kind of buffer overflow for using scanf ? Anyone pls help..
================
Edit: Matt T Heffron (code moved from comment below):
i fixed the logic,checked overflow problems but its still not executing...can someone explain...
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char a[20];
int n,c;
c=0;
printf("enter the size of the string ");
scanf("%d",&n);
printf("enter the string ");
fgets(a,n,stdin);
for(int i=0;i<(n-1)/2;i++)
{
if(a[i]==a[n-1-i])
{
c=0;
}
else
{
c=1;
break;
}
}
if(c==0)
printf("string is palindrome");
else
printf("string is not palindrome");
return 0;
}
解决方案
Um... try changing this:
scanf("%s",&a[20]);To this:
scanf("%s",a);Or even:
scanf("%s",&a[0]);
#include <stdio.h> #include <string.h> int main(){ char string1[20]; int i, length; int flag = 0; printf("Enter a string:"); scanf("%s", string1); length = strlen(string1); for(i=0;i < length ;i++){ if(string1[i] != string1[length-i-1]){ flag = 1; break; } } if (flag) { printf("%s is not a palindrome", string1); } else { printf("%s is a palindrome", string1); } return 0; }
Or with the help of string functions like strrev, strcmp:
#include <stdio.h> #include <string.h> int main() { char a[100], b[100]; printf("Enter the string to check if it is a palindrome\n"); gets(a); strcpy(b,a); strrev(b); if (strcmp(a,b) == 0) printf("Entered string is a palindrome.\n"); else printf("Entered string is not a palindrome.\n"); return 0; }
-KR
In addition of the mistake pointed by our Original, your code is logically flawed (it can assert the string is palindrome before the whole string is actually checked).
Try
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_LEN 20 int main() { char a[MAX_LEN+1]; printf("enter the string "); if ( fgets( a, MAX_LEN+1, stdin) ) { size_t len = strlen(a)-1; // get rid of '\n' size_t i; printf("n = %d\n", len); for(i=0;i<len/2;i++) { if (a[i]!=a[len-1-i] ) break; } if ( i == len/2) printf("\nstring is palindrome"); else printf("\nstring is not palindrome"); printf("\n"); } return 0; }
这篇关于检查字符串是否是回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文