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硬核学习资料👇👇👇👇
相关推荐
多喝开水少熬夜5 分钟前
Trie树相关算法题java实现
java·开发语言·算法
QT 小鲜肉7 分钟前
【QT/C++】Qt定时器QTimer类的实现方法详解(超详细)
开发语言·数据库·c++·笔记·qt·学习
lsx20240634 分钟前
MySQL WHERE 子句详解
开发语言
Tony Bai41 分钟前
【Go模块构建与依赖管理】09 企业级实践:私有仓库与私有 Proxy
开发语言·后端·golang
Lucky小小吴1 小时前
开源项目5——Go版本快速管理工具
开发语言·golang·开源
Mr.Jessy1 小时前
Web APIs 学习第五天:日期对象与DOM节点
开发语言·前端·javascript·学习·html
杨福瑞1 小时前
数据结构:单链表(2)
c语言·开发语言·数据结构
进化中的码农1 小时前
Go中的泛型编程和reflect(反射)
开发语言·笔记·golang
音符犹如代码2 小时前
Java并发List实战:CopyOnWriteArrayList原理与ArrayList常见面试题
java·开发语言·面试·list
又是忙碌的一天2 小时前
抽象类和接口
java·开发语言