C语言中各字符串函数的用法

当涉及到处理字符串和内存块时,C语言提供了一系列强大的标准库函数。在本文中,我们将介绍一些常见的字符串和内存操作函数,包括它们的用法和示例代码。

1. strlen - 计算字符串长度

strlen函数用于计算一个字符串的长度,即它包含的字符数(不包括字符串结束符\0)。

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

int main() {
    char str[] = "Hello, World!";
    int length = strlen(str);
    printf("Length of the string: %d\n", length);
    return 0;
}

2. strcpy - 复制字符串

strcpy函数用于将一个字符串复制到另一个字符串中。

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

int main() {
    char source[] = "Hello, World!";
    char destination[20];
    strcpy(destination, source);
    printf("Copied string: %s\n", destination);
    return 0;
}

3. strcat - 追加字符串

strcat函数用于将一个字符串追加到另一个字符串的末尾。

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

int main() {
    char str1[] = "Hello, ";
    char str2[] = "World!";
    strcat(str1, str2);
    printf("Concatenated string: %s\n", str1);
    return 0;
}

4. strcmp - 比较字符串

strcmp函数用于比较两个字符串。它返回0表示两个字符串相等,正数表示第一个字符串大于第二个字符串,负数表示第一个字符串小于第二个字符串。

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

int main() {
    char str1[] = "apple";
    char str2[] = "banana";
    int result = strcmp(str1, str2);
    
    if (result == 0) {
        printf("Strings are equal.\n");
    } else if (result < 0) {
        printf("str1 is less than str2.\n");
    } else {
        printf("str1 is greater than str2.\n");
    }
    
    return 0;
}

5. strncpy - 复制部分字符串

strncpy函数用于将源字符串的一部分复制到目标字符串中。

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

int main() {
    char source[] = "Hello, World!";
    char destination[20];
    strncpy(destination, source, 5);
    destination[5] = '\0'; // 手动添加字符串结束符
    printf("Copied string: %s\n", destination);
    return 0;
}

6. strncat - 追加部分字符串

strncat函数用于将源字符串的一部分追加到目标字符串的末尾。

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

int main() {
    char str1[] = "Hello, ";
    char str2[] = "World!";
    strncat(str1, str2, 3); // 仅追加前3个字符
    printf("Concatenated string: %s\n", str1);
    return 0;
}

7. strncmp - 比较部分字符串

strncmp函数用于比较两个字符串的一部分。它与strcmp类似,但只比较指定数量的字符。

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

int main() {
    char str1[] = "apple";
    char str2[] = "appetizer";
    int result = strncmp(str1, str2, 3); // 比较前3个字符
    
    if (result == 0) {
        printf("First 3 characters are equal.\n");
    } else {
        printf("First 3 characters are not equal.\n");
    }
    
    return 0;
}

8. strstr - 查找子字符串

strstr函数用于在一个字符串中查找另一个字符串。它返回第一次出现子字符串的位置。

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

int main() {
    char str[] = "Hello, World!";
    char sub[] = "World";
    char *result = strstr(str, sub);
    
    if (result != NULL) {
        printf("Substring found at position: %ld\n", result - str);
    } else {
        printf("Substring not found.\n");
    }
    
    return 0;
}

9. strtok - 分割字符串

strtok函数用于分割一个字符串为多个子字符串,通常用于解析文本数据。

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

int main() {
    char str[] = "apple,banana,cherry";
    char *token = strtok(str, ",");
    
    while (token != NULL) {
        printf("Token: %s\n", token);
        token = strtok(NULL, ","); // 以逗号分割下一个子字符串
    }
    
    return 0;
}

10. strerror - 获取错误信息

strerror函数用于根据错误代码获取错误信息的字符串。

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

int main() {
    int errnum = EIO; // 示例错误代码
    printf("Error message: %s\n", strerror(errnum));
    return 0;
}

11. memcpy - 复制内存块

memcpy函数用于将一个内存块的内容复制到另一个内存块。

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

int main() {
    char source[] = "Hello, World!";
    char destination[20];
    memcpy(destination, source, strlen(source) + 1);
    printf("Copied string: %s\n", destination);
    return 0;
}

12. memmove - 安全地复制内存块

memmove函数与memcpy类似,但它能够安全地处理重叠的内存块。

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

int main() {
    char str[] = "Hello, World!";
    memmove(str + 7, str, 5); // 将"World!"移到"Hello, "之后
    printf("Modified string: %s\n", str);
    return 0;
}

13. memcmp - 比较内存块

memcmp函数用于比较两个内存块的内容。

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

int main() {
    char block1[] = "Hello";
    char block2[] = "Helloworld";
    int result = memcmp(block1, block2, 5); // 比较前5个字节
    
    if (result == 0) {
        printf("Memory blocks are equal.\n");
    } else {
        printf("Memory blocks are not equal.\n");
    }
    
    return 0;
}

这些是一些常见的C标准库函数,用于处理字符串和内存块。它们在不同的情况下非常有用,可以帮助你有效地处理文本和数据。要注意,这些函数在使用时需要小心,以确保不会引发缓冲区溢出或其他内存相关的问题。 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值