回文,就是将头尾互换,地址位置不需改变。
#include <stdio.h>
#include <string.h>
const int TRUE = 1;
const int FALSE = 0;
typedef int STATUS;
typedef char ElementType;
typedef struct
{
int stack_size;
ElementType *top;
ElementType *base;
}STACK;
STATUS IsPlalindrome(ElementType* s);
int main()
{
int len=0;
ElementType *a=NULL;
printf("你要输入几个字符:");
scanf_s("%d", &len);
getchar();
a = (ElementType *)malloc(len * sizeof(ElementType));
printf("\n请输入待检测的字符串:");
gets(a);
if (IsPlalindrome(a))
printf("\n是回文字符串!");
else
printf("\n不是回文字符串!");
return 0;
}
STATUS IsPlalindrome(ElementType* s)
{
int len;
ElementType *p;
STACK stack;
len = strlen(s) / 2;
p = s;
stack.base = (ElementType *)malloc(len * sizeof(ElementType));
if (stack.base == NULL)
return FALSE;
stack.top = stack.base;
stack.stack_size = len;
for (int i = 0; i < stack.stack_size; i++)
{
*(stack.top) = *p;
stack.top++;
p++;
}
if (strlen(s) % 2 == 1)
{
p++;
}
while (stack.top != stack.base)
{
if (*(stack.top - 1) != *p)
{
free(stack.base);
return FALSE;
}
else
{
stack.top--;
p++;
}
}
if (stack.top == stack.base)
{
free(stack.base);
return TRUE;
}
return FALSE;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)