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);

原文地址:https://www.cnblogs.com/y-z-h/p/13933389.html