strcmp 与比较
比较字符串
strcmp 与比较 是 CoddyKit 上的免费 C Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C Academy 课程共包含 4 节课。
比较字符串
在 C 中,您不能使用 == 比较字符串。它只会比较两个数组的地址,而不是它们的内容。
请使用 <string.h> 中的 strcmp(a, b),逐个字符进行比较。
#include <stdio.h>
#include <string.h>
int main(void) {
char a[] = "apple";
char b[] = "apple";
if (strcmp(a, b) == 0)
printf("Strings are equal\n");
return 0;
}strcmp 的返回值
strcmp 返回一个 int:
- 如果两个字符串相等,则返回 0
- 如果
a中第一个不同的 char 小于b中对应的 char,则返回负数 - 其他情况返回正数
#include <stdio.h>
#include <string.h>
int main(void) {
printf("%d\n", strcmp("abc", "abc"));
printf("%d\n", strcmp("abc", "abd"));
printf("%d\n", strcmp("abd", "abc"));
return 0;
}“零表示相等”的陷阱
一个常见错误是编写 if (strcmp(a, b)),却以为它表示相等。
请记住:字符串相等时,strcmp 返回0(假值),因此该条件在字符串不相等时才为真。请始终显式地与 0 比较。
#include <stdio.h>
#include <string.h>
int main(void) {
char input[] = "yes";
if (strcmp(input, "yes") == 0)
printf("Confirmed\n");
else
printf("Not confirmed\n");
return 0;
}比较的工作方式
strcmp 会逐个比较字符的 ASCII(字节)值,直到发现不同字符或遇到终止符。
大写字母(A=65)排在小写字母(a=97)之前,因此 "Zebra" 的排序位置在 "apple" 之前。
#include <stdio.h>
#include <string.h>
int main(void) {
printf("%d\n", strcmp("Zebra", "apple"));
return 0;
}字典序
strcmp 返回值的正负号可以帮助您按字母顺序排列字符串。返回负数表示第一个参数在字典序中更靠前。
#include <stdio.h>
#include <string.h>
int main(void) {
const char *x = "banana";
const char *y = "cherry";
if (strcmp(x, y) < 0)
printf("%s comes before %s\n", x, y);
return 0;
}strncmp:限制比较范围
strncmp(a, b, n) 最多比较 n 个字符。这对于检查前缀很有用。
例如,可以测试字符串是否以 "http" 开头。
#include <stdio.h>
#include <string.h>
int main(void) {
char url[] = "https://site.com";
if (strncmp(url, "https", 5) == 0)
printf("Secure URL\n");
return 0;
}不区分大小写的比较
标准的 strcmp 区分大小写。若要忽略大小写,您可以在比较过程中使用 <ctype.h> 中的 tolower 转换字符。
(POSIX 系统还提供 strcasecmp,但它不属于 C 标准。)
#include <stdio.h>
#include <ctype.h>
int ci_cmp(const char *a, const char *b) {
while (*a && (tolower(*a) == tolower(*b))) { a++; b++; }
return tolower(*a) - tolower(*b);
}
int main(void) {
printf("%d\n", ci_cmp("Hello", "hello"));
return 0;
}使用比较进行查找
您可以遍历字符串数组,并使用 strcmp 查找匹配项,就像使用一个简单的查找表一样。
#include <stdio.h>
#include <string.h>
int main(void) {
const char *fruits[] = {"apple", "banana", "cherry"};
const char *target = "banana";
for (int i = 0; i < 3; i++) {
if (strcmp(fruits[i], target) == 0) {
printf("Found at index %d\n", i);
}
}
return 0;
}排序字符串
以 strcmp 作为比较引擎,您可以对字符串数组进行排序。下面是一个简短的冒泡排序示例,它会根据顺序交换指针。
#include <stdio.h>
#include <string.h>
int main(void) {
const char *names[] = {"Carol", "Alice", "Bob"};
int n = 3;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1 - i; j++)
if (strcmp(names[j], names[j+1]) > 0) {
const char *t = names[j];
names[j] = names[j+1];
names[j+1] = t;
}
for (int i = 0; i < n; i++) printf("%s\n", names[i]);
return 0;
}不要直接用减法进行比较
您可能会尝试使用 a[i] - b[i] 编写自己的比较逻辑,但对于原始 char 值,符号行为由具体实现决定,并且可能发生溢出。
请优先使用库函数 strcmp,它能正确处理这些边界情况。
#include <stdio.h>
#include <string.h>
int main(void) {
int r = strcmp("file1", "file2");
printf("Result sign: %s\n", r < 0 ? "negative" : "non-negative");
return 0;
}比较用户输入
一个实际应用是检查密码或菜单选项。读取输入后,使用 strcmp 将其与预期值进行比较。
请始终与 0 比较,以测试是否相等。
#include <stdio.h>
#include <string.h>
int main(void) {
char choice[] = "quit";
if (strcmp(choice, "quit") == 0)
printf("Exiting program\n");
return 0;
}快速检查
请测试您对 strcmp 返回值的理解。
回顾
您已经学习了如何在 C 中比较字符串:
- strcmp 相等时返回 0,排序比较时返回负数或正数。
- 绝不要对字符串使用
==;它比较的是地址。 - 请始终通过测试
== 0来判断相等(这是“零是假值”的陷阱)。 - strncmp 比较前缀;不区分大小写的比较需要使用
tolower。
常见问题解答
「strcmp 与比较」课时是免费的吗?
是的 — 「strcmp 与比较」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C Academy 课程的其余内容,请升级到 CoddyKit PRO。 C Academy 课程共包含 4 节课。
「strcmp 与比较」这节课中我会学到什么?
比较字符串 你通过在浏览器中直接运行的动手代码来练习 C Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 C Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 C Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「strcmp 与比较」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 C Academy 课中编写并运行代码吗?
能。每节 C Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。