回文串判定

回文串判定

Time Limit: 1000MS Memory limit: 65536K

题目描述

输入一串字符(长度小于100),判断该串字符是否是回文串(正序读与逆序读内容相同)。

输入

输入一串字符(长度小于100)。

输出

若该串字符是回文串输出“yes",否则输出”no“。

示例输入

asdfgfdsa

示例输出

yes

 

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define stackmax 10000
#define stacknum 10000
typedef int ElemType;
typedef struct
{
    ElemType *base;
    ElemType *top;
    int stacksize;
} SqStack;
int InitStack(SqStack &s)
{
    s.base = (ElemType*) malloc (stackmax*sizeof(ElemType));
    if (! s.base)
        exit(0);
    s.top = s.base;
    s.stacksize = stackmax;
    return 0;
}
char Push(SqStack &s , char e)
{
    if(s.top-s.base >= s.stacksize)
    {
        s.base = (ElemType *)realloc(s.base,(s.stacksize+stacknum)*sizeof(ElemType));
        if (! s.base ) exit(0);
        s.top = s.base + s.stacksize;
        s.stacksize += stacknum;
    }
    *s.top++=e;
}

int  Pop(SqStack &s)
{
    if(s.top == s.base)   return 0;
    s.top--;
    return 1;
}
int GetTop(SqStack &s)
{
    if(s.top == s.base)   return 0;
    char e=*(s.top-1);
    return e;
}
int StackEmpty(SqStack &s)
{
    if(s.top == s.base)
        return 1;
    else
        return 0;
}
 void putstack(SqStack &s)
{
    while(s.top > s.base)
    {
        printf("%d", *(s.top-1));
        s.top--;
    }
}
char judge(SqStack &s, char c[])
{
    int n=strlen(c), i;
    for(i=0; i<n/2; i++)
    {
        Push(s, c[i]);
    }
    if(n%2==0)
    {
        for(i=n/2;i<n;i++)
        {
            if(GetTop(s)==c[i])
                Pop(s);
            else
                break;
        }
    }
    if(n%2!=0)
    {
        for(i=n/2+1;i<n;i++)
        {
            if(GetTop(s)==c[i])
                Pop(s);
            else
                break;
        }
    }
    if(StackEmpty(s)) printf("yes");
    else printf("no");
}
int main()
{
    char c[105];
    SqStack s;
    gets(c);
    InitStack(s);
    judge(s, c);
    return 0;
}
 
 


回文字符串是一个特殊的字符序列,即正序读和倒序读完全相同的字符串。例如:"madam" 或者 "racecar"。在C语言中,我们经常处理字符串,并且需要一些方法来判断一个字符串是否是回文。这通常涉及到对字符串进行逐字符的比较。 以下是一个C语言函数用于检测字符串是否为回文: ```c #include <stdio.h> #include <stdbool.h> bool isPalindrome(const char *str) { int len = strlen(str); for(int i = 0; i < len / 2; ++i) { // 跳过非字母数字字符,并将其转换为小写 while (!isalnum(str[i]) && i < len - 1) i++; while (!isalnum(str[len-i-1]) && len-i-1 > 0) len--; if(tolower(str[i]) != tolower(str[len-i-1])) { return false; } } return true; } int main() { const char* str1 = "Madam"; const char* str2 = "racecar"; const char* str3 = "hello"; printf("'%s' is palindrome? %s\n", str1, isPalindrome(str1) ? "Yes" : "No"); printf("'%s' is palindrome? %s\n", str2, isPalindrome(str2) ? "Yes" : "No"); printf("'%s' is palindrome? %s\n", str3, isPalindrome(str3) ? "Yes" : "No"); return 0; } ``` 在这个程序中,首先导入了必要的头文件。`isPalindrome()` 函数接收一个指向字符串的指针作为参数。该函数计算字符串的长度,并从两端开始逐步向中心移动,比较对应的字符。如果在任何时候发现两个对应位置的字符不匹配,则返回false,表示输入的字符串不是一个回文。只有当所有的字符都匹配时,才会返回true。 运行此程序会显示以下结果: ``` 'Madam' is palindrome? Yes 'racescar' is palindrome? Yes 'hello' is palindrome? No ``` 以上是使用C语言判断字符串是否为回文的基本方法和代码实例。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值