zoukankan      html  css  js  c++  java
  • 字符串比较

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<math.h>
    #include<time.h>

    //使用系统函数strcmp、strncmp

    int main0101()

    {

    //ch1=ch2,则返回0;ch1<ch2,则返回负数;ch1>ch2,则返回正数 

      char ch1[]="hallo world";

      char ch2[]="hello world";

    //比较字符串

      //int value=strcmp(ch1,ch2);

    //比较前n个字符 

      //int value=strncmp(ch1,ch2,5)

      //printf("%d ",value);

    //把字符串是否相等作为条件判断

      if(!strcmp(ch1,ch2)

      {

        printf("相同 ");

      }

      else

      {

        printf("不相同 ");

      }

      return EXIT_SUCCESS;

    }

    //自定义函数strcmp

    int my_strcmp01(const char*s1,const char*s2)

    {

      while(*s1==*s2)

      {

        if(*s1=='')

        {

          return 0;

        }

        s1++;

        s2++;

      }

      return *s1<*s2 ? 1 : -1 ;

    }

    //自定义函数strncmp

    int my_strncmp(const char*s1,const char*s2,size_t n)

    {

      for(int i=0;i<n && s1[i] && s2[i] ; i++)

      {

        if(s1[i]!=s2[i])

        {

          return s1[i]>s2[i] ? 1:-1;

        }  

        return 0;

      }

    }

    int main()

    {

      char ch1[]="hallo world";

      char ch2[]="hello world";

    //自定义函数strcmp

      //int value=my_strcmp(ch1,ch2);

    //自定义函数strncmp

      int value=my_strncmp(ch1,ch2,5);

      printf("%d ",value);

      return 0;

    }

  • 相关阅读:
    Oracle—SQL基础语句
    ORACLE中数据类型
    ORACLE表结构的操作
    PLSQL--存储过程
    PLSQL--游标
    PLSQL --流程控制
    PLSQL --变量
    Ajax请求校验username是否可用
    jQuery学习笔记(四)使用选择器三
    jQuery学习笔记(三)使用选择器二
  • 原文地址:https://www.cnblogs.com/wanghong19991213/p/13610652.html
Copyright © 2011-2022 走看看