C 语言字符串判断函数详解18
字符串是 C 语言中用于表示文本和字符序列的数据类型。为了方便操作和处理字符串,C 语言提供了多种字符串判断函数,以下是对这些函数的详细
1. strcmp()
strcmp() 函数用于比较两个字符串是否相等。它返回一个整数,表示两个字符串的比较结果:
0:如果两个字符串相等
正数:如果第一个字符串大于第二个字符串
负数:如果第一个字符串小于第二个字符串
示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("str1 和 str2 相等");
} else if (result > 0) {
printf("str1 大于 str2");
} else {
printf("str1 小于 str2");
}
return 0;
}
```
2. strncmp()
strncmp() 函数与 strcmp() 类似,但它只比较指定数量的字符。它返回一个整数,表示两个字符串的比较结果:
0:如果两个字符串在指定数量的字符内相等
正数:如果第一个字符串在指定数量的字符内大于第二个字符串
负数:如果第一个字符串在指定数量的字符内小于第二个字符串
示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello World";
char str2[] = "Hello Earth";
int result = strncmp(str1, str2, 5);
if (result == 0) {
printf("str1 和 str2 在前 5 个字符内相等");
} else if (result > 0) {
printf("str1 在前 5 个字符内大于 str2");
} else {
printf("str1 在前 5 个字符内小于 str2");
}
return 0;
}
```
3. strcasecmp()
strcasecmp() 函数与 strcmp() 类似,但它忽略大小写。它返回一个整数,表示两个字符串的比较结果:
0:如果两个字符串在忽略大小写后相等
正数:如果第一个字符串在忽略大小写后大于第二个字符串
负数:如果第一个字符串在忽略大小写后小于第二个字符串
示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "hEllo";
int result = strcasecmp(str1, str2);
if (result == 0) {
printf("str1 和 str2 在忽略大小写后相等");
} else if (result > 0) {
printf("str1 在忽略大小写后大于 str2");
} else {
printf("str1 在忽略大小写后小于 str2");
}
return 0;
}
```
4. strncasecmp()
strncasecmp() 函数与 strncmp() 类似,但它忽略大小写并只比较指定数量的字符。它返回一个整数,表示两个字符串的比较结果:
0:如果两个字符串在忽略大小写并比较指定数量的字符后相等
正数:如果第一个字符串在忽略大小写并比较指定数量的字符后大于第二个字符串
负数:如果第一个字符串在忽略大小写并比较指定数量的字符后小于第二个字符串
示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello World";
char str2[] = "HeLLo eArTh";
int result = strncasecmp(str1, str2, 5);
if (result == 0) {
printf("str1 和 str2 在忽略大小写并比较前 5 个字符后相等");
} else if (result > 0) {
printf("str1 在忽略大小写并比较前 5 个字符后大于 str2");
} else {
printf("str1 在忽略大小写并比较前 5 个字符后小于 str2");
}
return 0;
}
```
5. strlen()
strlen() 函数用于获取字符串的长度。它返回一个整数,表示字符串中字符的数量(不包括终止符 '\0')。
示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
int length = strlen(str);
printf("str 的长度为 %d", length);
return 0;
}
```
6. strchr()
strchr() 函数用于在字符串中查找第一个出现的指定字符。它返回一个指向该字符的指针,如果找不到该字符,则返回 NULL。
示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
char *ptr = strchr(str, 'o');
if (ptr != NULL) {
printf("在 str 中找到第一个 'o' 字符");
} else {
printf("在 str 中未找到 'o' 字符");
}
return 0;
}
```
7. strstr()
strstr() 函数用于在字符串中查找第一个出现的指定子串。它返回一个指向该子串的指针,如果找不到该子串,则返回 NULL。
示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
char *ptr = strstr(str, "World");
if (ptr != NULL) {
printf("在 str 中找到子串 'World'");
} else {
printf("在 str 中未找到子串 'World'");
}
return 0;
}
```
C 语言提供了丰富的字符串判断函数,可以满足各种字符串比较和操作需求。通过熟练掌握这些函数,程序员可以高效地处理字符串数据,编写出功能强大的程序。
2025-02-12
上一篇:C 语言自定义函数传地址
Java编译疑难杂症:深入解析非法字符及其解决方案
https://www.shuihudhg.cn/134528.html
Python 配置管理:深度解析 `configparser` 模块,高效读取与解析 .conf/.ini 文件
https://www.shuihudhg.cn/134527.html
Python字符串与列表的高效连接:深度解析、性能优化与最佳实践
https://www.shuihudhg.cn/134526.html
PHP 应用数据库性能优化:从代码到架构的全方位指南
https://www.shuihudhg.cn/134525.html
PHP函数可变参数的艺术:深度解析与实战技巧
https://www.shuihudhg.cn/134524.html
热门文章
C 语言中实现正序输出
https://www.shuihudhg.cn/2788.html
c语言选择排序算法详解
https://www.shuihudhg.cn/45804.html
C 语言函数:定义与声明
https://www.shuihudhg.cn/5703.html
C语言中的开方函数:sqrt()
https://www.shuihudhg.cn/347.html
C 语言中字符串输出的全面指南
https://www.shuihudhg.cn/4366.html