字符串函数

#include <string.h>

   size_t strlen(const char *s);

strlen;

#include <stdio.h>
#include <string.h>

int my_strlen(char *s) {
    int cunt = 0;
    int i = 0;
    while(s[i] != '\0') {
        i++;
        cunt++;
    }
    return cunt;
}
int main() {
    char line[] = "hello world";
    printf("strlen = %lu\n", strlen(line));
    printf("sizeof() = %lu\n", sizeof(line));
    printf("my_strlen = %d\n", my_strlen(line));
    return 0;
}

strcmp
在这里插入图片描述


#include <stdio.h>
#include <string.h>

int my_cmp(char *s1, char *s2) {
   /* int i = 0;
    while(1) {
        if (s1[i] != s2[i]) {
            break;
        }
        else if (s1[i] == '\0') {
            break;
        }
        i++;

    }
    return s1[i] - s2[i];
    */
    while(*s1 == *s2 && *s1 != '\0') {
        s1++;
        s2++;
    }
    return *s1 -*s2;
} 
int main() {
    char s1[] = "abcdefg ";
    char s2[] = "abcdefgh ";
    printf("%d\n", strcmp(s1, s2));
    printf("%d\n", my_cmp(s1, s2));
    printf("%d\n", 'a'-'A');
    printf("%d\n", ' ' - 'h');
    return 0;
}

strcpy
在这里插入图片描述

在这里插入图片描述

strchr
在这里插入图片描述

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
    char s[] = "hello";
    char *p = strchr(s, 'l');
    printf("%s\n", p);
    char *q = strchr(p + 1, 'l');
    char c = *p;
    *p = '\0';
    char *t = (char*)malloc(strlen(p) + 1);
    t = strcpy(t, s);
    printf("%s\n", q);
    printf("t = %s\n", t);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值