这个函数接口实在简单,直接编写几个实例学习怎么使用
// `strcmp` 是 C 标准库中的字符串比较函数,用于比较两个字符串是否相同。其原型为:
// int strcmp(const char *s1, const char *s2);
// 其中,`s1` 和 `s2` 分别指向需要进行比较的两个字符串。
// 函数返回值是一个整型,用于表示两个字符串的大小关系。
// 如果 `s1` 比 `s2` “小”,则返回值为负数;
// 如果 `s1` 比 `s2` “大”,则返回值为正数;
// 如果两个字符串相等,则返回值为 0。
// 实例1. 比较两个固定字符串是否相等
// 该代码比较了两个固定字符串 "hello" 和 "world" 是否相等。
// `strcmp` 函数返回非 0 值,因此打印的结果是 "hello != world"。
#include <stdio.h>
#include <string.h>
int main()
{
char s1[] = "hello";
char s2[] = "world";
int result = strcmp(s1, s2);
if (result < 0) {
printf("%s < %s\n", s1, s2);
} else if (result == 0) {
printf("%s == %s\n", s1, s2);
} else {
printf("%s > %s\n", s1, s2);
}
return 0;
}
// strcmp实例2. 比较两个用户输入的字符串是否相等
// 该代码要求用户输入两个字符串,然后比较它们是否相等。
#include <stdio.h>
#include <string.h>
int main() {
char s1[20];
char s2[20];
printf("Enter string1: ");
scanf("%s", s1);
printf("Enter string2: ");
scanf("%s", s2);
int result = strcmp(s1, s2);
if (result < 0) {
printf("%s < %s\n", s1, s2);
} else if (result == 0) {
printf("%s == %s\n", s1, s2);
} else {
printf("%s > %s\n", s1, s2);
}
return 0;
}
// strcmp实例3. 使用 `strcmp` 函数对字符串进行排序
// 该代码通过 `qsort` 函数对一个字符串数组进行排序并输出。
// 其中,比较函数 `cmp` 对应上面讲述的 `strcmp` 函数。
// 其余部分的主要作用是动态分配内存并释放相应的资源。
// 运行该程序时,需要输入需要排序的字符串个数和每个字符串本身,最后按照字典序输出排序后的结果。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void *a, const void *b)
{
return strcmp(*(char **)a, *(char **)b);
}
int main()
{
int n, i;
scanf("%d", &n);
char **strs = (char **) malloc(n * sizeof(char *));
for (i = 0; i < n; ++i) {
char *str = (char *) malloc(16 * sizeof(char));
scanf("%s", str);
strs[i] = str;
}
qsort(strs, n, sizeof(char *), cmp);
printf("%s", strs[0]);
for (i = 1; i < n; ++i) {
printf(" %s", strs[i]);
}
printf("\n");
for (i = 0; i < n; ++i) {
free(strs[i]);
}
free(strs);
return 0;
}
自定义一个字串比较函数实例:
// 自定义的模拟 `strcmp` 函数实现
// 该函数比较两个字符串的字符是否相等,直到出现不相等的字符。
// 如果相等的长度相等,则保证的是返回 0 表示没有不相等的字符。
// 需要注意的是,在比较的时候需要同时判断两个字符串是否到达了自己的末尾,否则会出现越界错误。
// 下面的代码演示了如何在 `main` 函数中测试自定义的 `my_strcmp` 函数。
// 将两个字符串传递作为参数分别进行比较,然后输出比较结果。
// 在我们的测试程序中,我们对两组字符串进行了比较,
// 第一组为 "hello" 和 "world",第二组为 "abab" 和 "abba"。输出结果为:
// ```
// hello != world
// abab < abba
// ```
// 我们可以看到,自定义的模拟 `strcmp` 函数的结果与标准库中的 `strcmp` 函数结果相同,这说明我们的函数工作正常。
// 需要注意的是,实际的 `strcmp` 函数可能比我们实现的要复杂,因为它不仅考虑了大小写等因素,还具有处理多字节字符集的能力。
// 因此,在实际的应用中,我们仍然应该使用标准库提供的 `strcmp` 函数。
#include <stdio.h>
int my_strcmp(const char *s1, const char *s2)
{
int i = 0;
while (s1[i] != '\0' && s2[i] != '\0' && s1[i] == s2[i]) {
++i;
}
return s1[i] - s2[i];
}
int main()
{
char s1[] = "hello";
char s2[] = "world";
int result = my_strcmp(s1, s2);
if (result == 0) {
printf("%s == %s\n", s1, s2);
} else {
printf("%s != %s\n", s1, s2);
}
char s3[] = "abab";
char s4[] = "abba";
result = my_strcmp(s3, s4);
if (result == 0) {
printf("%s == %s\n", s3, s4);
} else if (result < 0) {
printf("%s < %s\n", s3, s4);
} else {
printf("%s > %s\n", s3, s4);
}
return 0;
}
// 自定义的模拟 `strcmp` 函数实现
#include <stdio.h>
#include <stdlib.h>
// 为什么在mystrcmp中做指针偏移,不会影响main函数中s1,s2指向的内存位置呢?
// 这是因为在 C 语言中,函数的参数是以值传递的方式进行的。
// 也就是说,当我们在 `main` 函数中调用 `mystrcmp` 函数时,
// 实际上是将指向字符串 `s1` 和 `s2` 内存地址的值复制给了 `mystrcmp` 函数中对应的参数。
// 因此,`mystrcmp` 函数中的指针 `s1` 和 `s2` 的指向和移动只影响了其本身,
// 而并没有影响 `main` 函数中的字符串 `s1` 和 `s2`。
// 需要注意的是,尽管 `mystrcmp` 函数中的指针变量没有直接影响到 `main` 函数中的变量,
// 但是仍然可以通过指针操作来间接修改 `main` 函数中的变量。
// 也就是修传入内存中保存的内容,原来的指针访问同样的位置时内容也时被修改后的数据
// 例如,如果我们在 `mystrcmp` 函数中对指针解引用并修改其存储的值,那么 `main` 函数中的变量也会受到影响。
// 因此,在 C 语言中,必须小心地处理指针操作,以避免出现意外的副作用。
// 更简单一点的理解:
// mystrcmp中会将原来传入的指针拷贝赋值给形参const char *s1,s2,
// 然后形参在mystrcmp函数中进行偏移,原来的指针内存位置当然不会发生改变
// 如果需要改变原来指针的位置,就得传入指针的指针
int mystrcmp(const char *s1, const char *s2)
{
printf("mystrcmp 00 s1 : %p, s2 : %p\n", s1, s2);
while(*s1 == *s2 && *s1 != '\0' && *s2 != '\0'){
s1++;
s2++;
}
printf("mystrcmp 01 s1 : %p, s2 : %p\n", s1, s2);
return *s1 - *s2;
}
int main()
{
// malloc函数使用时需要添加stdlib.h,否则编译会报错
char *s1 = (char *)malloc(100);
char *s2 = (char *)malloc(100);
printf("main 00 s1 : %p, s2 : %p\n", s1, s2);
printf("input s1 & s2 :\n");
scanf("%s%s", s1, s2);
int result = mystrcmp(s1, s2);
printf("main 01 s1 : %p, s2 : %p\n", s1, s2);
printf("\nafter strcmp :\n");
if (result < 0) {
printf("%s < %s\n", s1, s2);
} else if (result == 0) {
printf("%s == %s\n", s1, s2);
} else {
printf("%s > %s\n", s1, s2);
}
return 0;
}