写一个函数,输入一个字符串,判断这个字符串是否是回文(例如:abcba, sadfgwewgfdas,12433421等)

用C语言编程

第1个回答  2010-11-08
#include<iostream>
#include<string.h>
using namespace std;

int huiwen(char str[])
{
int i=0;
int flag = 0;
while((i<= (int)strlen(str)/2) && (flag == 0))
{
if(*(str+i)==*(str + (strlen(str)-i) - 1)) /////////
{
i++;
}
else
flag = 1;
}
return flag == 0 ? 1 : 0; //////////
}

int main()
{
char str[100];
gets(str);
cout << huiwen(str) << endl;
return 0;
}
输出:
123456
0
Press any key to continue

54345
1
Press any key to continue本回答被网友采纳
第2个回答  2010-11-08
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
bool IsPalindrome(const char* psz)
{
if (NULL == psz)
return false;
int nLen = strlen(psz);
int nMid = (nLen + 1)/2;
for (int i = 0; i < nMid; i++)
{
if (*(psz+i) != *(psz+nLen-1-i))
return false;
}

return true;
}
int main(int argc, char* argv[])
{
printf("请输入字符串:\n");
char*psz = new char[1024];
memset(psz,0,1024);
scanf("%s",psz);
if (IsPalindrome(psz))
printf("回文字符串\n");
else
printf("非回文字符串\n");

system("pause");
return 0;
}
第3个回答  2010-11-08
这是我以前编的,你修改一下就行了。
#include "stdio.h"
#include "math.h"
void main()
{ long i,j,k,a,b;
for(i=1;i<10;i++)
for(j=0;j<10;j++)
{a=100*i+10*j+i;
b=sqrt(a);
if(a==b*b)
printf("%d\n",a);}
for(i=1;i<10;i++)
for(j=0;j<10;j++)
for(k=0;k<10;k++)
{a=10000*i+1000*j+100*k+10*j+i;
b=sqrt(a);
if(a==b*b)
printf("%ld\n",a);}
}

2本回答被提问者采纳
相似回答