C语言字符串函数和内存函数

我们在学习C语言的时候会用到字符串函数和内存函数,但是它们的作用和限制条件你是否都清楚呢?

字符串函数和内存函数在C语言中都是非常常用的库函数,具有以下用途:

1. 字符串函数

1.1 strlen:用于计算一个字符串的长度,也就是该字符串中实际字符的个数(不包括结尾的空字符'\0')。可以用于确定字符串的存储空间大小,或者用于检查字符串是否符合要求。

1.2 strcpy:用于将一个字符串复制到另一个字符串中。可以用于将一段字符序列存储到缓冲区中,或者用于将一个字符串的值传递给另一个字符串。

1.3 strcat:用于将两个字符串连接成一个字符串。可以用于拼接字符串,或者用于将一段字符序列添加到另一个字符串的末尾。

1.4 strcmp:用于比较两个字符串的大小关系。可以用于比较字符串是否相等,或者按字典序比较字符串。

1.5 strstr:用于在一个字符串中查找另一个字符串。可以用于查找字符串中特定的子字符串,或者用于文本处理和搜索。

1.6 strtok:用于将一个字符串分解成一系列子字符串。可以用于解析命令行参数,或者读取文件中的行。

2. 内存函数

2.1 memcpy:用于将一个内存区域的内容复制到另一个内存区域。可以用于复制任意类型的数据,包括基本类型、结构体和指针。

2.2 memset:用于将一个内存区域的内容设置为特定的值。可以用于初始化内存,或者清除敏感数据。

2.3 memcmp:用于比较两个内存区域的大小关系。可以用于比较任意类型的数据,包括基本类型、结构体和指针。

2.4 memmove:用于将一个内存区域的内容移动到另一个内存区域。可以用于处理重叠的内存区域,或者实现数据结构的插入和删除操作。

总的来说,字符串函数和内存函数都是C语言中非常常用的函数库,可以用于处理字符串和内存相关的操作。在实际使用时,需要注意参数的正确性和内存安全问题,以免发生错误或安全漏洞。

3.函数原型及作用

1.字符串函数

1.1 strlen

函数原型:

size_t strlen(const char *str);

函数作用:

计算字符串 str 的长度,不包括空字符 \0

示例代码:

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

int main() {
    char str[] = "Hello, world!";
    size_t len = strlen(str);
    printf("The length of the string is %zu.\n", len);
    return 0;
}

输出:

The length of the string is 13.

1.2 strcpy

函数原型:

char *strcpy(char *dest, const char *src);

函数作用:

将字符串 src 复制到 dest 中,并返回 dest

示例代码:

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

int main() {
    char src[] = "Hello, world!";
    char dest[20];
    strcpy(dest, src);
    printf("The copied string is %s.\n", dest);
    return 0;
}

输出:

The copied string is Hello, world!.

1.3 strcat

函数原型:

char *strcat(char *dest, const char *src);

函数作用:

将字符串 src 连接到字符串 dest 的末尾,并返回 dest

示例代码:

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

int main() {
    char dest[20] = "Hello, ";
    char src[] = "world!";
    strcat(dest, src);
    printf("The concatenated string is %s.\n", dest);
    return 0;
}

输出:

The concatenated string is Hello, world!.

1.4 strcmp

函数原型:

int strcmp(const char *str1, const char *str2);

函数作用:

比较字符串 str1 和 str2,如果 str1 小于 str2,则返回负数;如果 str1 大于 str2,则返回正数;如果 str1 和 str2 相等,则返回 0。

示例代码:

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

int main() {
    char str1[] = "Hello, world!";
    char str2[] = "Hello, World!";
    int result = strcmp(str1, str2);
    if (result < 0) {
        printf("String 1 is less than string 2.\n");
    } else if (result > 0) {
        printf("String 1 is greater than string 2.\n");
    } else {
        printf("String 1 is equal to string 2.\n");
    }
    return 0;
}

输出:

String 1 is less than string 2.

1.5 strstr

函数原型:

char *strstr(const char *str1, const char *str2);

函数作用:

在字符串 str1 中查找字符串 str2,如果找到了,则返回 str2 在 str1 中的首次出现位置的指针;如果没有找到,则返回空指针。

示例代码:

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

int main() {
    char str1[] = "Hello, world!";
    char str2[] = "world";
    char *result = strstr(str1, str2);
    if (result == NULL) {
        printf("String not found.\n");
    } else {
        printf("String found at position %ld.\n", result - str1);
    }
    return 0;
}

输出:

String found at position 7.

1.6 strtok

函数原型:

char *strtok(char *str, const char *delim);

函数作用:

将字符串 str 分解为一系列子字符串,其中每个子字符串都由分隔符 delim 分隔。第一次调用时,需要将 str 作为参数传入;之后每次调用时,需要将第一个参数设置为 NULL。

示例代码:

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

int main() {
    char str[] = "one,two,three,four,five";
    char *token = strtok(str, ",");
    while (token != NULL) {
        printf("%s\n", token);
        token = strtok(NULL, ",");
    }
    return 0;
}

输出:

one
two
three
four
five

2.内存函数

2.1 memcpy

函数原型:

void *memcpy(void *dest, const void *src, size_t n);

函数作用:

将 src 指向的内存区域的前 n 个字节复制到 dest 指向的内存区域中。

示例代码:

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

int main() {
    char src[] = "Hello, world!";
    char dest[20];
    memcpy(dest, src, strlen(src) + 1);
    printf("The copied string is %s.\n", dest);
    return 0;
}

输出:

The copied string is Hello, world!.

2.2 memset

函数原型:

void *memset(void *s, int c, size_t n);

函数作用:

将 s 指向的内存区域的前 n 个字节设置为值 c

示例代码:

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

int main() {
    char str[20] = "Hello, world!";
    memset(str + 7, '*', 5);
    printf("The modified string is %s.\n", str);
    return 0;
}

输出:

The modified string is Hello, *****!.

2.3 memcmp

函数原型:

int memcmp(const void *s1, const void *s2, size_t n);

函数作用:

比较 s1 和 s2 指向的内存区域的前 n 个字节。如果 s1 小于 s2,则返回负数;如果 s1 大于 s2,则返回正数;如果 s1 和 s2 相等,则返回 0。

示例代码:

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

int main() {
    char str1[] = "Hello, world!";
    char str2[] = "Hello, World!";
    int result = memcmp(str1, str2, strlen(str1));
    if (result < 0) {
        printf("Memory 1 is less than memory 2.\n");
    } else if (result > 0) {
        printf("Memory 1 is greater than memory 2.\n");
    } else {
        printf("Memory 1 is equal to memory 2.\n");
    }
    return 0;
}

输出:

Memory 1 is less than memory 2.

2.4 memmove

函数原型:

void *memmove(void *dest, const void *src, size_t n);

函数作用:

将 src 指向的内存区域的前 n 个字节移动到 dest 指向的内存区域中。如果 src 和 dest 指向的内存区域重叠,则移动后的结果是未定义的。

示例代码:

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

int main() {
    char str[] = "Hello, world!";
    memmove(str + 7, str + 2, 5);
    printf("The modified string is %s.\n", str);
    return 0;
}

输出:

The modified string is Hello, worod!.

以上就是字符串函数和内存函数的详解及示例代码。需要注意的是,使用这些函数时需要注意内存安全问题,避免发生内存泄漏或越界等问题。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值