zoukankan      html  css  js  c++  java
  • 判断一字符串是不是对称的

    Q: 函数checkstr判断一字符串是不是对称的。其中msg为输入的字符串,对称返回0,不对称返回-1,实现该函数。
            int checkstr(const char *msg);

     代码实现如下:

           

     1 #include <stdio.h>
     2 #include <assert.h>
     3 #include <string.h>
     4 
     5 int checkstr(const char *msg)
     6 {
     7     int len=strlen(msg);
     8     assert(msg!=NULL && len>1);    
     9     int low=0;
    10     int high=len-1;
    11 
    12     while(low<=high)
    13     {
    14         if (msg[low]==msg[high])
    15         {
    16             low++;
    17             high--;
    18         }
    19         else
    20         {
    21             break;
    22         }
    23     }
    24     if (low>high)
    25     {
    26         return 0;
    27     }
    28     else
    29     {
    30         return -1;
    31     }
    32 }
    33 
    34 void main()
    35 {
    36     char *str="abcyicba";
    37     int result=checkstr(str);
    38     printf("%d",result);
    39 }
  • 相关阅读:
    JVM内存的划分
    劝学
    java中switch的用法
    方法传递参数的分类
    ajax缓存机制
    vuex
    keep-alive
    路由滚动行为scrollBehavior
    vue等
    防止刷新路由后参数消失
  • 原文地址:https://www.cnblogs.com/Trony/p/2648743.html
Copyright © 2011-2022 走看看