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


     1 #include <string.h>
     2 bool palindrome( char *s ){
     3     int n=strlen(s);
     4     int num=0;
     5     if(n%2==0){
     6         int cnt=n/2;
     7         for(int i=0;i<cnt;i++){
     8             if(s[i]==s[n-1-i]){
     9                 num++;
    10             }
    11         }
    12         if(num==cnt){
    13             return true;
    14         }
    15         else{
    16             return false;
    17         }
    18     }
    19     if(n%2!=0){
    20         int cnt=(n+1)/2;
    21         for(int i=0;i<cnt;i++){
    22             if(s[i]==s[n-1-i]){
    23                 num++;
    24             }
    25         }
    26         if(num==cnt){
    27             return true;
    28         }
    29         else{
    30             return false;
    31         }
    32     }
    33 }
  • 相关阅读:
    [转]深入理解Flash Player重绘
    type tips
    textfield tips
    HTML5---3.表单新增的type属性
    HTML5---2.语义化标签的兼容性问题以及解决方案
    HTML5---14.自定义标签
    HTML5---1.语义化标签
    第三章 DOM
    《将博客搬至CSDN》
    一些简单的编程练习题
  • 原文地址:https://www.cnblogs.com/samgue/p/13246449.html
Copyright © 2011-2022 走看看