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硬核学习资料👇👇👇👇
相关推荐
graceyun几秒前
初阶数据结构(C语言实现)——6.1插入排序详解(思路图解+代码实现)
c语言·数据结构·排序算法
努力的飛杨26 分钟前
学习记录-js进阶-性能优化
开发语言·javascript·学习
星零零43 分钟前
【Java】链表(LinkedList)(图文版)
java·开发语言·数据结构·经验分享·笔记·链表
行思理1 小时前
php 要达到go的性能,应该如何优化php
开发语言·php·运维开发
不爱原创的Yoga1 小时前
【中间件】Rabbit离线部署操作
开发语言·中间件·ruby
啊吧怪不啊吧1 小时前
C++相关基础概念之入门讲解(下)
开发语言·数据结构·c++
(❁´◡`❁)Jimmy(❁´◡`❁)1 小时前
1201. 【高精度练习】蜜蜂路线
开发语言·c++·算法
潘多编程1 小时前
Spring Boot 3 新特性实战:从理论到实践
java·开发语言·网络
八了个戒2 小时前
「JavaScript深入」Socket.IO:基于 WebSocket 的实时通信库
开发语言·前端·javascript·websocket
锐策2 小时前
『 C++ 』多线程编程中的参数传递技巧
开发语言·c++