zoukankan      html  css  js  c++  java
  • 习题8-8 判断回文字符串(20 分)

    本题要求编写函数,判断给定的一串字符是否为“回文”。所谓“回文”是指顺读和倒读都一样的字符串。如“XYZYX”和“xyzzyx”都是回文。

    函数接口定义:

    bool palindrome( char *s );
    

    函数palindrome判断输入字符串char *s是否为回文。若是则返回true,否则返回false

    裁判测试程序样例:

    #include <stdio.h>
    #include <string.h>
    
    #define MAXN 20
    typedef enum {false, true} bool;
    
    bool palindrome( char *s );
    
    int main()
    {
        char s[MAXN];
    
        scanf("%s", s);
        if ( palindrome(s)==true )
            printf("Yes
    ");
        else
            printf("No
    ");
        printf("%s
    ", s);
    
        return 0;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例1:

    thisistrueurtsisiht
    

    输出样例1:

    Yes
    thisistrueurtsisiht
    

    输入样例2:

    thisisnottrue
    

    输出样例2:

    No
    thisisnottrue
    
    #include <stdio.h>
    #include <string.h>
    
    #define MAXN 20
    typedef enum {false, true} bool;
    /*typedef在计算机编程语言中用来为复杂的声明定义简单的别名。
    这里typedef enum{0,1} bool;就是对枚举型类型enum{0,1}定义别名为bool,
    
    定义之后,你若想定义一个enum{0,1}类型的枚举变量,则可以简单的bool a;即可。
    */
    
    bool palindrome( char *s );
    
    int main()
    {
        char s[MAXN];
    
        scanf("%s", s);
        if ( palindrome(s)==true )
            printf("Yes
    ");
        else
            printf("No
    ");
        printf("%s
    ", s);
    
        return 0;
    }
    
    /* 你的代码将被嵌在这里 */
    bool palindrome( char *s )
    
    {
        int i,n;
        n=strlen(s);//字符串长度 
        if(n%2==1)//如果长度为奇数的话 
        {
            for(i=0;i<(n-1)/2;i++)//前一半长度 
            {
                if(s[i]==s[n-1-i])//第一个等于最后一个,第二个等于倒数第二个 
                continue;
                else
                return false;
            }
        }
    
        else//如果长度为偶数的话 
        {
            for(i=0;i<n/2;i++)//前一半长度 
            {
                if(s[i]==s[n-1-i])//
                continue;
                else
                return false;
            }
        }
        return true;
    }
  • 相关阅读:
    CentOS 7部署KVM之三基本管理
    CentOS 7部署KVM之二安装配置
    CentOS 7部署KVM之一架构介绍
    DOM 事件流
    渐进增强与优雅降级
    (三)跟我一起玩Linux网络服务:DHCP服务配置之主服务器配置
    (二)跟我一起玩Linux网络服务:BIND的自动部署(附上完整的代码)
    责任链模式--行为模式
    装饰模式--- 结构型模式
    elastic-job+zookeeper实现分布式定时任务调度的使用(springboot版本)
  • 原文地址:https://www.cnblogs.com/2228212230qq/p/9268543.html
Copyright © 2011-2022 走看看