用C编写一个程序,输入5个字符串,输出其中最大的字符串
时间: 2024-05-01 22:20:45 浏览: 10
以下是一个用C语言编写的程序,可以输入5个字符串并输出其中最大的字符串:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[5][50], max_str[50];
int i;
// 输入5个字符串
for (i = 0; i < 5; i++) {
printf("请输入第%d个字符串:", i + 1);
scanf("%s", str[i]);
}
// 假设第一个字符串为最大字符串
strcpy(max_str, str[0]);
// 比较字符串,找到最大字符串
for (i = 1; i < 5; i++) {
if (strcmp(str[i], max_str) > 0) {
strcpy(max_str, str[i]);
}
}
// 输出最大字符串
printf("最大的字符串是:%s\n", max_str);
return 0;
}
```
运行程序后,会提示输入5个字符串,每输入一个字符串后按下回车键即可输入下一个字符串。程序会自动比较这5个字符串,并输出其中最大的字符串。
相关问题
用c语言编写程序,输入5个字符串,输出其中最大的字符串。
以下是用C语言实现输入5个字符串,输出其中最大的字符串的程序:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[5][30];
int i, max = 0;
printf("请输入5个字符串:\n");
for (i = 0; i < 5; i++)
{
scanf("%s", str[i]);
if (strlen(str[i]) > strlen(str[max]))
max = i;
}
printf("最大的字符串是:%s\n", str[max]);
return 0;
}
```
首先定义一个5行30列的字符数组,用于存储五个字符串。
然后使用for循环输入五个字符串,并比较它们的长度,找到其中最大的字符串。
最后输出最大的字符串即可。
用c语言编写一个程序 输入一个字符串 输出其中所出现过的大写英文字母
下面是一个示例程序,可以实现输入一个字符串,然后输出其中所有出现过的大写英文字母:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int count[26] = {0};
int i, len;
printf("请输入一个字符串:\n");
fgets(str, 100, stdin); // 从标准输入读取字符串
len = strlen(str);
for (i = 0; i < len; i++) {
if (isupper(str[i])) { // 如果当前字符是大写字母
count[str[i] - 'A']++; // 统计出现次数
}
}
printf("出现过的大写英文字母有:\n");
for (i = 0; i < 26; i++) {
if (count[i] > 0) {
printf("%c ", 'A' + i); // 输出字母
}
}
return 0;
}
```
上述程序中,我们定义了一个字符数组 `str`,用来存储用户输入的字符串。然后我们定义了一个整型数组 `count[26]`,用来统计每个大写字母出现的次数。变量 `i` 用来循环遍历字符串中的每个字符,变量 `len` 是字符串的长度。
在循环中,我们首先使用 `isupper` 函数判断当前字符是否是大写字母。如果是,就通过 `count` 数组统计出现次数。
最后,我们再次循环输出出现过的大写英文字母。
注意,我们使用了 `fgets` 函数来从标准输入读取字符串,而不是 `scanf` 函数。这是因为 `fgets` 可以一次读取整行字符串,而 `scanf` 只能读取一个单词,容易导致输入不完整的问题。