zoukankan      html  css  js  c++  java
  • C语言字符串处理

    字符串和字符串数组声明和初始化

        char buff[1024] = {''};
        char *str1;

    字符串和字符串数组间的转化

        
    /* 字符数组可以直接转字符串,但字符串不能直接转字符数组 */
    char buff[1024] = "successing to read the file!"; char *str1; str1 = buff;

    字符串和字符串数组长度量取

    size_t strlen(char const *str)
        char buff[1024] = "successing to read the file!";
        char *str1;
        int len = 0;
        str1 = buff;
        len = strlen(str1);

    复制字符串

    char *strcpy(char const *cpystr, char const *str)
        char buff[1024] = "successing to read the file!";
        char *str1,*cpystr;
        int len = 0;
        str1 = buff;
        strcpy(cpystr, str1);

    连接字符串

     char *strcat(char const *str1, char const *str2)
        char buff[1024] = "successing to read the file!";
        char *str1,*p;
        char *chs = "to";
        str1 = buff;
        p = strcat(str1, chs);
    
        printf("str1 = %s
    ", str1);
        printf("chs = %s
    ", chs);
        printf("p = %s
    ", p);

     比较字符串

    char *strcmp(char const *str1, char const *str2)
        char buff[1024] = "successing to read the file!";
        char *str1,*str2;
        char *chs = "successing";
        int num = 0;
        str1 = buff;
        strcpy(str1, str2);
    
        num = strcmp(str1, chs);
        printf("num1 = %d
    ", num);
        num = strcmp(str1, str2);
        printf("num2 = %d
    ", num);
        num = strcmp(chs, str1);
        printf("num3 = %d
    ", num);

    在一个字符串中查找一个字符

    /* 从左往右查找,第一次出现 */
    char
    *strchr(char const *str, int ch)
    /* 从右往左查找,第一次出现 */
    char
    *strchr(char const *str, int ch)
        char buff[1024] = "successing to read the file!";
        char *str1,*lp,*rp;
        char ch = 'c';
        str1 = buff;
        lp = strchr(str1, ch);
        rp = strrchr(str1, ch);

    在一个字符串中查找任意几个个字符

    在一个字符串中查找一个子字符串

    char *strstr(char const *str, char const *chs)
        char buff[1024] = "successing to read the file!";
        char *str1,*p;
        char *chs = "to";
        str1 = buff;
        p = strstr(str1, chs);
    
        printf("str1 = %s
    ", str1);
        printf("p = %s
    ", p);

  • 相关阅读:
    js获取当前时间,日期格式为年月日
    create-react-app里添加less
    react css 模块化
    react 点击父级元素 不触发子级元素click事件
    react-router-dom 实现路由左右滑动进入页面的效果
    vue路由左右侧滑动 react路由左右侧滑动
    react 父组件不能直接在子组件上加className,也得用props传递过去
    react 父组件调用子组件方法
    css滚动相关问题记录
    javascript异步编程的几种方法
  • 原文地址:https://www.cnblogs.com/y-z-h/p/13933389.html
Copyright © 2011-2022 走看看