常用字符串函数实现及使用

目录

strlen函数

strcpy函数

strncpy函数

strcat函数

strncat函数

strcmp函数

strncmp函数

strstr函数

strtok函数

strerror函数


strlen函数

strlen函数计算字符串长度

int strlen(const char *str) {
    const char *s = str;
    // 逐个遍历字符串中的字符,直到遇到 null 终止符
    while (*s) {
        s++;
    }
    // 返回字符个数,不包括 null 终止符
    return (s - str);
}

strcpy函数

传参两个字符数组把一个字符数组的内容拷贝到另一个字符数组中

#include 

char* strcpy(char* destination, const char* source) {
    char* start = destination; // 保存目标字符串的起始地址

    // 将源字符串的内容逐个复制到目标字符串,直到遇到 '\0'
    while (*destination++ = *source++) ) {
        ;
}
    return start; // 返回目标字符串的起始地址
}

strncpy函数

长度受限制的拷贝函数,功能与strcpy一样都是拷贝数组,只是限制了拷贝个数

char *strncat(char *destination, const char *source, size_t num) {
    // 定义两个指针,用于遍历 destination 和 source 字符串
    char *dest_ptr = destination;
    const char *src_ptr = source;

    // 将 dest_ptr 移动到 destination 字符串的末尾
    while (*dest_ptr != '\0') {
        dest_ptr++;
    }

    // 将 source 字符串中的字符逐个复制到 destination 字符串的末尾
    // 同时限制最多复制 num 个字符
    while (*src_ptr != '\0' && num > 0) {
        *dest_ptr = *src_ptr;
        dest_ptr++;
        src_ptr++;
        num--;
    }

    // 在 destination 字符串的末尾添加 '\0',表示字符串结束
    *dest_ptr = '\0';

    return destination;
}

strcat函数

追加字符串函数,就是在源字符串数组后面追加一个字符串

char* strcat(char* destination, const char* source) {
    // 定义两个指针,用于遍历 destination 和 source 字符串
    char* dest_ptr = destination;
    const char* src_ptr = source;

    // 将 dest_ptr 移动到 destination 字符串的末尾
    while (*dest_ptr != '\0') {
        dest_ptr++;
    }

    // 将 source 字符串中的字符逐个复制到 destination 字符串的末尾
    while (*src_ptr != '\0') {
        *dest_ptr = *src_ptr;
        dest_ptr++;
        src_ptr++;
    }

    // 在 destination 字符串的末尾添加 '\0',表示字符串结束
    *dest_ptr = '\0';

    return destination;
}

strncat函数

同样加n限制长度,这里限制的是追加字符的个数

char *strncat(char *destination, const char *source, size_t num) {
    // 定义两个指针,用于遍历 destination 和 source 字符串
    char *dest_ptr = destination;
    const char *src_ptr = source;

    // 将 dest_ptr 移动到 destination 字符串的末尾
    while (*dest_ptr != '\0') {
        dest_ptr++;
    }

    // 将 source 字符串中的字符逐个复制到 destination 字符串的末尾
    // 同时限制最多复制 num 个字符
    while (*src_ptr != '\0' && num > 0) {
        *dest_ptr = *src_ptr;
        dest_ptr++;
        src_ptr++;
        num--;
    }

    // 在 destination 字符串的末尾添加 '\0',表示字符串结束
    *dest_ptr = '\0';

    return destination;
}

strcmp函数

比较两个字符串是否相等

int strcmp(const char *s1, const char *s2) {
    // 循环比较两个字符串的字符,直到遇到不相等的字符或者遇到字符串结束符 '\0'
    while (*s1++ == *s2++) {
      ;
    }

    // 返回两个字符的差值,即字符 ASCII 码的差值
    return *s1 - *s2;
}

strncmp函数

strncmp 函数是 C 语言标准库中的一个函数。作用与strcmp一样,strcmp 函数不同的是,strncat 函数允许指定比较的字符数目,以避免缓冲区溢出问题。

int strncmp(const char *s1, const char *s2, size_t num) {
    // 逐个比较两个字符串的字符,直到达到最大比较字符数目或者遇到字符串结束符 '\0'
    while (num > 0 && *s1 != '\0' && *s2 != '\0' && *s1 == *s2) {
        s1++;
        s2++;
        num--;
    }

    // 如果 num 为 0,则表示已经比较了指定数量的字符,返回 0
    if (num == 0) {
        return 0;
    }

    // 返回两个字符的差值,即字符 ASCII 码的差值
    return *s1 - *s2;
}

strstr函数

strstr 函数用于在一个字符串中查找指定子字符串的第一次出现的位置。如果找到了子字符串,则返回子字符串在原字符串中的地址;如果未找到,则返回 NULL。

char *strstr(const char *haystack, const char *needle) {
    // 如果 needle 为空字符串,直接返回 haystack
    if (*needle == '\0') {
        return (char *)haystack;
    }

    // 在 haystack 中逐个比较字符,直到找到与 needle 的第一个字符匹配的位置
    while (*haystack != '\0') {
        // 保存当前 haystack 的指针位置
        const char *haystack_ptr = haystack;
        const char *needle_ptr = needle;

        // 比较 haystack 和 needle 的字符,直到 needle 的字符全部匹配或者 haystack 到达末尾
        while (*needle_ptr != '\0' && *haystack_ptr == *needle_ptr) {
            haystack_ptr++;
            needle_ptr++;
        }

        // 如果 needle 的字符全部匹配,返回匹配的位置
        if (*needle_ptr == '\0') {
            return (char *)haystack;
        }

        // 如果当前 haystack 的字符与 needle 的第一个字符不匹配,继续向后移动 haystack
        haystack++;
    }

    // 如果 haystack 中找不到与 needle 匹配的子字符串,返回 NULL
    return NULL;
}

strtok函数

strtok 函数用于将字符串分割成一系列子字符串。它在首次被调用时,接受一个字符串作为参数,并使用指定的分隔符将字符串分割成一个个子字符串。之后,每次调用该函数时,它会返回分割后的下一个子字符串,直到所有子字符串都被提取完毕。

注意点:strtok 函数会修改它的第一个参数,即原始字符串,通过在分隔符处插入 null 字符来分割字符串。所以一般拷贝一份副本传参过去,第一次使用正常传参,后面传空指针,因为静态变量已经记录了。

char *my_strtok(char *str, const char *delim) {
    static char *last_ptr = NULL; // 保存上一次 strtok 调用后的位置

    // 如果传入的 str 是 NULL,继续从上次的位置继续分割
    if (str == NULL) {
        str = last_ptr;
    }

    // 跳过 str 前面的分隔符
    while (*str != '\0' && strchr(delim, *str) != NULL) {
        str++;
    }

    // 如果已经到达字符串末尾,返回 NULL
    if (*str == '\0') {
        last_ptr = NULL;
        return NULL;
    }

    // 记录当前分割的起始位置
    char *token_start = str;

    // 找到分隔符或者字符串末尾的位置
    while (*str != '\0' && strchr(delim, *str) == NULL) {
        str++;
    }

    // 如果找到了分隔符,用 null 字符替换它
    if (*str != '\0') {
        *str = '\0';
        last_ptr = str + 1;
    } else {
        last_ptr = NULL;
    }

    return token_start;
}

strerror函数

strerror 函数用于返回一个描述错误代码的字符串。通常,它将错误代码转换为对应的错误消息。这个函数在 C 标准库中提供,并且通常是与全局变量 errno 一起使用的。

示例:

#define _CRT_SECURE_NO_WARNINGS 1 
#pragma warning(distable:4996) 
#include 
#include 
#include 
int main() {
    FILE* file;

    // 尝试打开一个不存在的文件
    file = fopen("nonexistentfile.txt", "r");

    if (file == NULL) {
        // 如果文件打开失败,输出相应的错误消息
        printf("错误信息: %s\n", strerror(errno));
    }
    else {
        // 如果文件成功打开,关闭文件
        fclose(file);
    }

    return 0;
}

你可能感兴趣的:(c语言)