c语言:输入俩个不包含空格的字符串,判断俩个由大小写字母组成的字符串在忽略大小写后是否相等,输出相等或不等
时间: 2024-01-02 07:09:35 浏览: 50
可以使用C语言中的字符串函数,如`strcmpi()`函数来实现忽略大小写的字符串比较。具体实现如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("请输入第一个字符串:");
scanf("%s", str1);
printf("请输入第二个字符串:");
scanf("%s", str2);
if (strcmpi(str1, str2) == 0) {
printf("相等\n");
} else {
printf("不相等\n");
}
return 0;
}
```
其中`strcmpi()`函数会忽略字符串中字母的大小写,若相等则返回0,否则返回非0值。
相关问题
用c语言实现:输入两个不包含空格的字符串,判断两个由大小写字母组成的字符串在忽略大小写后是否相等。 注意:由于gcc编译器不能使用gets函数,可以使用scanf()函数。
可以使用strcmp()函数进行忽略大小写比较,但是该函数在不同系统中的实现可能存在差异,因此可以自行编写一个忽略大小写的字符串比较函数。
以下是一个示例代码:
```c
#include <stdio.h>
#include <ctype.h>
int strcasecmp(const char *s1, const char *s2) {
int i;
for (i = 0; s1[i] != '\0' && s2[i] != '\0'; i++) {
if (tolower(s1[i]) != tolower(s2[i])) {
return tolower(s1[i]) - tolower(s2[i]);
}
}
return s1[i] - s2[i];
}
int main() {
char s1[100], s2[100];
printf("请输入字符串1:");
scanf("%s", s1);
printf("请输入字符串2:");
scanf("%s", s2);
if (strcasecmp(s1, s2) == 0) {
printf("两个字符串在忽略大小写后相等\n");
} else {
printf("两个字符串在忽略大小写后不相等\n");
}
return 0;
}
```
该程序中使用了自定义的strcasecmp()函数来比较两个字符串是否相等,忽略了大小写。输入的字符串通过scanf()函数获取,而不是使用gets()函数,以避免缓冲区溢出等问题。
用c语言输入一个字符串,统计其中某个字符出现的次数,如果该字符为字母在统计时不区分大小写。
好的,我可以回答您的问题。以下是用C语言实现的代码示例,可以统计一个字符串中某个字符出现的次数,并且不区分大小写。
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int count_char(char *str, char ch)
{
int count = 0;
int len = strlen(str);
int i;
char c;
for (i = 0; i < len; ++i)
{
c = str[i];
if (tolower(c) == tolower(ch))
{
count++;
}
}
return count;
}
int main()
{
char str[100];
char ch;
int count;
printf("请输入一个字符串:\n");
scanf("%s", str);
printf("请输入要统计的字符:\n");
scanf(" %c", &ch);
count = count_char(str, ch);
printf("字符 %c 出现的次数为 %d。\n", ch, count);
return 0;
}
```
该程序使用了一个 `count_char` 函数来统计某个字符在字符串中出现的次数。函数中通过 `tolower` 函数将字符转换为小写形式,从而实现了不区分大小写的统计。
在 `main` 函数中,首先读入一个字符串和一个字符,然后调用 `count_char` 函数来统计字符出现的次数,并将计算结果打印输出。
注意,在输入字符时需要在 `%c` 前面加空格来忽略之前的换行符。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)