输入:第一行和第二行给出带空格的、区分大小写的英文字母,输出:第二行独有的英文字母,按顺序、可重复。c语言
时间: 2023-02-07 10:47:38 浏览: 73
以下是一个使用 C 语言的解决方案:
#include <stdio.h>
#include <string.h>
int main(void) {
char line1[100], line2[100];
int i, j, found;
// 读入两行字符串
scanf("%[^\n]%*c", line1);
scanf("%[^\n]%*c", line2);
// 遍历第一行字符串中的每一个字符
for (i = 0; i < strlen(line1); i++) {
// 在第二行中查找该字符是否出现过
found = 0;
for (j = 0; j < strlen(line2); j++) {
if (line1[i] == line2[j]) {
found = 1;
break;
}
}
// 如果该字符在第二行中没有出现过,则输出该字符
if (!found) {
printf("%c ", line1[i]);
}
}
return 0;
}
相关问题
给出代码:输入一行字符,分别统计并输出其中的英文字母、空格、数字和其它字符的个数。
以下是C语言代码实现:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i, letters = 0, spaces = 0, digits = 0, others = 0;
printf("请输入一行字符:\n");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
letters++;
} else if (isspace(str[i])) {
spaces++;
} else if (isdigit(str[i])) {
digits++;
} else {
others++;
}
}
printf("字母个数:%d\n", letters);
printf("空格个数:%d\n", spaces);
printf("数字个数:%d\n", digits);
printf("其他字符个数:%d\n", others);
return 0;
}
```
代码中使用了`fgets()`函数来读取一行字符,然后使用循环遍历每个字符,通过`isalpha()`、`isspace()`和`isdigit()`函数来判断字符的类型,最后统计各种类型字符的个数并输出。
c语言输入一行字符,分别统计出其中的大小写英文字母、空格、数字和其他字符的个数
这是一个C语言程序,可以输入一行字符并统计其中的大小写英文字母、空格、数字和其他字符的个数。具体实现方法如下:
```
#include <stdio.h>
int main()
{
char c;
int letters = 0, space = 0, digit = 0, other = 0;
while ((c = getchar()) != '\n')
{
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
letters++;
else if (c >= '0' && c <= '9')
digit++;
else if (c == ' ')
space++;
else
other++;
}
printf("字母数:%d\n空格数:%d\n数字数:%d\n其他字符:%d\n", letters, space, digit, other);
return 0;
}
```
程序中使用了一个while循环,每次读入一个字符并进行判断,如果是大小写英文字母,则字母数加1;如果是数字,则数字数加1;如果是空格,则空格数加1;否则其他字符数加1。最后使用printf函数输出统计结果。