C语言本关任务:输入一个字符串,包含不超过100个字符,编程实现找出字符串中最大字符元素并输出该元素及其对应的ASCII值. 读入字符串可以使用gets函数 输出格式要求为: "The largest character of \"%s\" is \'%c\' ,The ASCII is %d."
时间: 2023-08-03 15:06:54 浏览: 74
好的,以下是C语言的示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[101];
printf("请输入字符串:");
gets(str); // 读入字符串
char max_char = str[0];
int max_ascii = (int)str[0];
for (int i = 1; i < strlen(str); i++) {
if (str[i] > max_char) {
max_char = str[i];
max_ascii = (int)str[i];
}
}
printf("The largest character of \"%s\" is '%c', The ASCII is %d.", str, max_char, max_ascii);
return 0;
}
```
同样地,你只需要将以上代码复制到你的C编译器中编译运行,然后输入一个字符串,就可以得到输出结果。注意:不要输入超过100个字符的字符串,否则会出现错误。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)