一、字符串的基本概念
字符串的存储结构
在 C 语言中,字符串本质上是 字符数组,并以 空字符 \0
结尾。
存储特点:
- 每个字符占一个字节:
- 在 ASCII 编码中,一个字符(如
A
或 a
)占用 1 个字节。
- 一个字符串的长度是字符个数加 1(空字符
\0
)。
- 连续存储:
- 字符数组中的每个字符都连续存储在内存中,直到
\0
为止。
- 只读字符串:
- 用双引号定义的字符串常量,如
"Hello"
,通常存储在只读内存中,无法直接修改。
示例:定义与初始化字符串
| #include <stdio.h> |
| |
| int main() { |
| char str1[] = "Hello"; |
| char str2[] = {'H', 'e', 'l', 'l', 'o', '\0'}; |
| |
| printf("str1: %s\n", str1); |
| printf("str2: %s\n", str2); |
| |
| |
| char str3[] = {'H', 'e', 'l', 'l', 'o'}; |
| printf("str3: %s\n", str3); |
| return 0; |
| } |
二、字符串的输入与输出
示例:字符串输入与输出
| #include <stdio.h> |
| |
| int main() { |
| char name[50]; |
| |
| |
| printf("请输入你的名字:"); |
| scanf("%s", name); |
| printf("Hello, %s!\n", name); |
| |
| |
| printf("请输入一句话:"); |
| getchar(); |
| fgets(name, sizeof(name), stdin); |
| printf("你输入的是:%s", name); |
| |
| return 0; |
| } |
注意:fgets
读取的字符串会包含换行符,必要时可以手动去掉:
| name[strcspn(name, "\n")] = '\0'; |
字符串常见错误及防范
-
未初始化的字符串:
- 如果定义了字符数组但没有初始化,可能会包含垃圾值。
| char str[10]; |
| printf("%s\n", str); |
-
忘记空字符 \0
:
| char str[] = {'H', 'e', 'l', 'l', 'o'}; |
| printf("%s\n", str); |
-
数组越界访问:
| char str[5] = "Test"; |
| str[5] = 'X'; |
三、字符串操作函数
C 标准库提供了许多操作字符串的函数,以下以具体示例讲解常用的函数。
1. 字符串长度:strlen
| #include <stdio.h> |
| #include <string.h> |
| |
| int main() { |
| char str[] = "Hello, C!"; |
| size_t len = strlen(str); |
| printf("字符串 \"%s\" 的长度是:%lu\n", str, len); |
| return 0; |
| } |
2. 字符串复制:strcpy
和 strncpy
| #include <stdio.h> |
| #include <string.h> |
| |
| int main() { |
| char src[] = "Source"; |
| char dest[20]; |
| |
| |
| strcpy(dest, src); |
| printf("复制结果:%s\n", dest); |
| |
| |
| strncpy(dest, "Example", 3); |
| dest[3] = '\0'; |
| printf("部分复制结果:%s\n", dest); |
| |
| return 0; |
| } |
3. 字符串连接:strcat
和 strncat
| #include <stdio.h> |
| #include <string.h> |
| |
| int main() { |
| char str1[50] = "Hello"; |
| char str2[] = ", World!"; |
| |
| |
| strcat(str1, str2); |
| printf("拼接后:%s\n", str1); |
| |
| |
| strncat(str1, " C Programming", 3); |
| printf("部分拼接后:%s\n", str1); |
| |
| return 0; |
| } |
4. 字符串比较:strcmp
和 strncmp
| #include <stdio.h> |
| #include <string.h> |
| |
| int main() { |
| char str1[] = "apple"; |
| char str2[] = "banana"; |
| |
| int cmp = strcmp(str1, str2); |
| if (cmp < 0) |
| printf("\"%s\" 小于 \"%s\"\n", str1, str2); |
| else if (cmp > 0) |
| printf("\"%s\" 大于 \"%s\"\n", str1, str2); |
| else |
| printf("\"%s\" 等于 \"%s\"\n", str1, str2); |
| |
| |
| cmp = strncmp("abcde", "abcfg", 3); |
| printf("strncmp 比较结果:%d\n", cmp); |
| |
| return 0; |
| } |
5. 字符串查找:strchr
和 strstr
| #include <stdio.h> |
| #include <string.h> |
| |
| int main() { |
| char str[] = "Hello, World!"; |
| |
| |
| char *pos = strchr(str, 'W'); |
| if (pos) |
| printf("找到字符 'W',位置:%ld\n", pos - str); |
| |
| |
| char *substr = strstr(str, "World"); |
| if (substr) |
| printf("找到子串 \"World\",起始位置:%ld\n", substr - str); |
| |
| return 0; |
| } |
四、综合练习
示例 1:统计字符串中的字符个数
| #include <stdio.h> |
| #include <string.h> |
| |
| int main() { |
| char str[100]; |
| printf("请输入一个字符串:"); |
| fgets(str, sizeof(str), stdin); |
| |
| |
| str[strcspn(str, "\n")] = '\0'; |
| |
| printf("字符串 \"%s\" 的长度是:%lu\n", str, strlen(str)); |
| return 0; |
| } |
示例 2:字符串逆序
| #include <stdio.h> |
| #include <string.h> |
| |
| void reverse(char *str) { |
| int len = strlen(str); |
| for (int i = 0; i < len / 2; i++) { |
| char temp = str[i]; |
| str[i] = str[len - i - 1]; |
| str[len - i - 1] = temp; |
| } |
| } |
| |
| int main() { |
| char str[100]; |
| printf("请输入一个字符串:"); |
| fgets(str, sizeof(str), stdin); |
| |
| |
| str[strcspn(str, "\n")] = '\0'; |
| |
| reverse(str); |
| printf("逆序后的字符串是:%s\n", str); |
| return 0; |
| } |
总结
- 逐步练习: 先熟悉基本操作(输入输出),再学习字符串操作函数。
- 重点注意: 字符串数组需要足够的空间,并要防止输入过长。
- 实际应用: 多练习一些常见题目,如字符串统计、翻转、查找等。
五、字符串的实际应用
1. 字符串处理
字符串在 文本处理、加密、数据存储 等场景中非常常见。
示例:统计字符串中某个字符出现的次数
| #include <stdio.h> |
| #include <string.h> |
| |
| int countChar(const char *str, char ch) { |
| int count = 0; |
| while (*str) { |
| if (*str == ch) count++; |
| str++; |
| } |
| return count; |
| } |
| |
| int main() { |
| char str[100]; |
| printf("请输入一个字符串:"); |
| fgets(str, sizeof(str), stdin); |
| |
| char ch; |
| printf("请输入要统计的字符:"); |
| scanf(" %c", &ch); |
| |
| printf("字符 '%c' 出现了 %d 次。\n", ch, countChar(str, ch)); |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步