C语言中的string.h库函数用法详解

C语言中的 string.h 库提供了一组用于字符串处理的函数。这些函数可以帮助我们对字符串进行各种操作,如复制、连接、比较、搜索等。本文将详细介绍这些函数及其用法。

1. 概述

string.h 库包含了许多常用的字符串处理函数,如字符串复制、连接、比较、查找、长度计算等。这些函数可以简化字符串操作,提高编程效率。

2. 常用字符串处理函数

2.1 字符串长度

size_t strlen(const char *str)

计算字符串 str 的长度(不包括终止符 \0)。

示例代码:

1
2
3
4
5
6
7
8
9
#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;
}

2.2 字符串复制

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

将字符串 src 复制到 dest

示例代码:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <string.h>

int main() {
char src[] = "Hello, World!";
char dest[50];
strcpy(dest, src);
printf("Destination string is: %s\n", dest);
return 0;
}

char *strncpy(char *dest, const char *src, size_t n)

将字符串 src 的前 n 个字符复制到 dest

示例代码:

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <string.h>

int main() {
char src[] = "Hello, World!";
char dest[50];
strncpy(dest, src, 5);
dest[5] = '\0'; // 确保字符串以null终止
printf("Destination string is: %s\n", dest);
return 0;
}

2.3 字符串连接

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

将字符串 src 连接到 dest 的末尾。

示例代码:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <string.h>

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

char *strncat(char *dest, const char *src, size_t n)

将字符串 src 的前 n 个字符连接到 dest 的末尾。

示例代码:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <string.h>

int main() {
char dest[50] = "Hello";
char src[] = ", World!";
strncat(dest, src, 6);
printf("Concatenated string is: %s\n", dest);
return 0;
}

2.4 字符串比较

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

比较字符串 str1str2

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("The 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;
}

int strncmp(const char *str1, const char *str2, size_t n)

比较字符串 str1str2 的前 n 个字符。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "Hello";
char str2[] = "Helium";
int result = strncmp(str1, str2, 3);
if (result == 0) {
printf("The first 3 characters of the strings are equal.\n");
} else if (result < 0) {
printf("The first 3 characters of str1 are less than str2.\n");
} else {
printf("The first 3 characters of str1 are greater than str2.\n");
}
return 0;
}

2.5 字符串查找

char *strchr(const char *str, int c)

在字符串 str 中查找字符 c 的第一次出现,返回指向该字符的指针。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
char *ptr = strchr(str, 'W');
if (ptr) {
printf("Character found at position: %ld\n", ptr - str);
} else {
printf("Character not found.\n");
}
return 0;
}

char *strrchr(const char *str, int c)

在字符串 str 中查找字符 c 的最后一次出现,返回指向该字符的指针。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
char *ptr = strrchr(str, 'o');
if (ptr) {
printf("Last occurrence of character found at position: %ld\n", ptr - str);
} else {
printf("Character not found.\n");
}
return 0;
}

char *strstr(const char *haystack, const char *needle)

在字符串 haystack 中查找字符串 needle 的第一次出现,返回指向该字符串的指针。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h>

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

2.6 字符串拷贝

char *strdup(const char *s)

复制字符串 s 并返回一个指向新字符串的指针。需要注意的是,返回的新字符串是动态分配的,使用完后需要使用 free 函数释放。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
char str[] = "Hello, World!";
char *copy = strdup(str);
if (copy) {
printf("Duplicated string: %s\n", copy);
free(copy); // 释放动态分配的内存
} else {
printf("Failed to duplicate string.\n");
}
return 0;
}

2.7 字符串长度

size_t strnlen(const char *s, size_t maxlen)

计算字符串 s 的长度,但最多检查 maxlen 个字符。如果在 maxlen 个字符之前找到了终止符,则返回实际长度;否则返回 maxlen

示例代码:

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
size_t len = strnlen(str, 5);
printf("The length of the string (up to 5 characters) is: %zu\n", len);
return 0;
}

3. 总结

C语言中的 string.h 库函数提供了丰富的字符串处理功能,极大地简化了编程中的字符串操作。这些函数涵盖了字符串复制、连接、比较、查找、长度计算等多种常用操作。掌握这些函数的用法,可以大大提高编程效率和代码的可读性。

希望本文对你理解和使用 string.h 库函数有所帮助。如果你有任何疑问或建议,欢迎在评论区留言讨论。

作者

Xiongyuqi

发布于

2024-06-30

更新于

2024-06-30

许可协议

评论