c语言比较两字符串忽略大小写
时间: 2023-06-22 18:34:33 浏览: 85
在 C 语言中,可以使用标准库函数 `strcasecmp()` 来比较两个字符串,忽略大小写。这个函数的头文件是 `strings.h`。使用方法如下:
```c
int strcasecmp(const char *s1, const char *s2);
```
其中,`s1` 和 `s2` 是要比较的两个字符串。函数返回值为 0,表示两个字符串相等;返回值小于 0,表示 `s1` 小于 `s2`;返回值大于 0,表示 `s1` 大于 `s2`。
下面是一个使用示例:
```c
#include <stdio.h>
#include <strings.h>
int main() {
char str1[] = "Hello, world!";
char str2[] = "heLLo, World!";
if (strcasecmp(str1, str2) == 0) {
printf("The two strings are equal.\n");
} else {
printf("The two strings are not equal.\n");
}
return 0;
}
```
输出结果为:
```
The two strings are equal.
```
相关问题
c语言 libxml2 字符串比较 不忽略大小写
在 C 语言中,可以使用 `strcasecmp()` 函数进行字符串比较,该函数不区分大小写。但是,libxml2 库中没有提供 `strcasecmp()` 函数,因此我们可以使用标准的 C 字符串函数 `stricmp()` 来进行字符串比较。
下面是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
int main()
{
xmlDocPtr doc;
xmlNodePtr root;
xmlChar *value;
// 创建一个 XML 文档
doc = xmlNewDoc(BAD_CAST "1.0");
root = xmlNewNode(NULL, BAD_CAST "root");
xmlDocSetRootElement(doc, root);
// 添加一个元素
xmlNewChild(root, NULL, BAD_CAST "name", BAD_CAST "XML");
// 查找元素
xmlNodePtr node = xmlDocGetRootElement(doc);
node = node->children;
while (node != NULL) {
if (xmlStrcasecmp(node->name, BAD_CAST "NAME") == 0) { // 不区分大小写比较
value = xmlNodeListGetString(doc, node->children, 1);
printf("Found element: %s\n", value);
break;
}
node = node->next;
}
// 释放资源
xmlFree(value);
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
```
在上面的代码中,我们使用了 `xmlStrcasecmp()` 函数进行字符串比较。该函数与标准的 C 字符串函数 `strcasecmp()` 功能相同,都是忽略大小写进行比较。如果您需要进行大小写区分的比较,可以使用 `xmlStrcmp()` 函数。
忽略大小写的比较字符串算法C语言实现
忽略大小写的比较字符串可以通过将字符串中的所有字符转换为小写或大写来实现,然后进行比较。下面是一个C语言实现忽略大小写比较字符串的示例代码:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int stricmp(const char *s1, const char *s2) {
while (*s1 != '\0' && *s2 != '\0') {
int c1 = tolower(*s1);
int c2 = tolower(*s2);
if (c1 != c2) {
return c1 - c2;
}
s1++;
s2++;
}
return *s1 - *s2;
}
int main() {
char s1[] = "Hello, World!";
char s2[] = "heLLO, wORLD!";
if (stricmp(s1, s2) == 0) {
printf("The two strings are equal.\n");
} else {
printf("The two strings are not equal.\n");
}
return 0;
}
```
在上面的示例代码中,我们使用了`stricmp()`函数来比较两个字符串,该函数使用`tolower()`函数将字符串中的字符转换为小写进行比较。如果两个字符串相等,则返回0,否则返回它们之间的差值。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)