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 }
  • 相关阅读:
    Lombok介绍、使用方法和总结
    Vargant centOS7安装
    Nginx
    Docker
    GOPATH
    Golang http
    /^正则表达式$/
    go: missing Git command. See https://golang.org/s/gogetcmd
    Golang 反射
    Golang 常量
  • 原文地址:https://www.cnblogs.com/Trony/p/2648743.html
Copyright © 2011-2022 走看看