C语言字符串处理

在 C 语言中,字符串是以字符数组的形式表示的,以空字符 '\0' 结尾。C 语言提供了一系列的字符串处理函数,可以用于字符串的操作、查找、比较等。以下是一些常用的 C 语言字符串处理函数:

1. 字符串定义与初始化

c 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    // 字符数组表示字符串
    char str1[10] = "Hello";
    char str2[] = {'W', 'o', 'r', 'l', 'd', '\0'};

    // 字符串常量
    char *str3 = "C Programming";

    printf("str1: %s\n", str1);
    printf("str2: %s\n", str2);
    printf("str3: %s\n", str3);

    return 0;
}

2. 字符串长度

c 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, World!";
    int len = strlen(str);

    printf("Length of str: %d\n", len);

    return 0;
}

3. 字符串拷贝

c 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char source[] = "Hello, World!";
    char destination[20];

    // 使用 strcpy 进行拷贝
    strcpy(destination, source);

    printf("Source: %s\n", source);
    printf("Destination: %s\n", destination);

    return 0;
}

4. 字符串连接

c 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello, ";
    char str2[] = "World!";

    // 使用 strcat 进行连接
    strcat(str1, str2);

    printf("Concatenated string: %s\n", str1);

    return 0;
}

5. 字符串比较

c 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "apple";
    char str2[] = "banana";

    // 使用 strcmp 进行比较
    int result = strcmp(str1, str2);

    if (result < 0) {
        printf("str1 is less than str2\n");
    } else if (result > 0) {
        printf("str1 is greater than str2\n");
    } else {
        printf("str1 is equal to str2\n");
    }

    return 0;
}

6. 字符串查找

c 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, World!";
    char *ptr;

    // 使用 strstr 进行查找
    ptr = strstr(str, "World");

    if (ptr != NULL) {
        printf("Substring found at position: %ld\n", ptr - str);
    } else {
        printf("Substring not found\n");
    }

    return 0;
}

7. 字符串分割

C 语言本身不提供直接的字符串分割函数,但可以使用 strtok 函数来实现字符串分割。

c 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "apple,orange,banana";
    char *token;

    // 使用 strtok 进行分割
    token = strtok(str, ",");

    while (token != NULL) {
        printf("Token: %s\n", token);
        token = strtok(NULL, ",");
    }

    return 0;
}

8. 字符串转换

字符串转整数

使用 atoistrtol 函数将字符串转换为整数。

c 复制代码
#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[] = "12345";
    int num1 = atoi(str);

    // 或者使用 strtol
    char *endptr;
    long num2 = strtol(str, &endptr, 10);

    printf("Converted Integer (atoi): %d\n", num1);
    printf("Converted Integer (strtol): %ld\n", num2);

    return 0;
}
整数转字符串

使用 sprintfsnprintf 函数将整数转换为字符串。

c 复制代码
#include <stdio.h>

int main() {
    int num = 12345;
    char str1[20];
    char str2[20];

    // 使用 sprintf
    sprintf(str1, "%d", num);

    // 或者使用 snprintf
    snprintf(str2, sizeof(str2), "%d", num);

    printf("Converted String (sprintf): %s\n", str1);
    printf("Converted String (snprintf): %s\n", str2);

    return 0;
}

9. 字符串操作的安全性考虑

在进行字符串操作时,要注意防止缓冲区溢出和安全漏洞。推荐使用带有长度参数的安全版本函数。

  • strncpy 替代 strcpy
  • strncat 替代 strcat
  • strncmp 替代 strcmp
c 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char dest[10] = "Hello";
    char src[] = "World";

    // 使用 strncpy 避免缓冲区溢出
    strncpy(dest, src, sizeof(dest) - 1);
    dest[sizeof(dest) - 1] = '\0';

    printf("Safe Concatenation: %s\n", dest);

    return 0;
}

10. 动态内存分配和释放字符串

使用 mallocfree 进行字符串的动态内存分配和释放。

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char *str;

    // 动态分配内存
    str = (char *)malloc(20);

    if (str == NULL) {
        perror("Memory allocation failed");
        exit(EXIT_FAILURE);
    }

    // 操作字符串
    strcpy(str, "Dynamic Memory");

    // 使用字符串

    // 释放内存
    free(str);

    return 0;
}

在实际的 C 语言编程中,字符串处理是非常常见的任务之一。除了上述介绍的函数外,还有许多其他字符串处理函数,开发者可以根据具体需求选择合适的函数进行使用。

复制代码
      👇👇👇👇点击最下方名片: Linux兵工厂,免费领取Linux硬核学习资料👇👇👇👇
相关推荐
艾莉丝努力练剑34 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
倔强青铜35 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼6 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上6 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang6 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc6 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
AI视觉网奇6 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀6 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
liulilittle6 小时前
.NET ExpandoObject 技术原理解析
开发语言·网络·windows·c#·.net·net·动态编程