對每行輸入字串(僅含字母,長度≤100),找出ASCII碼最大的字元,在每個最大字元後插入 (max)。
max_charmax_char 則補輸出 (max)#include <stdio.h>
#include <string.h>
int main() {
char s[101];
while (fgets(s, sizeof(s), stdin)) {
int len = strlen(s);
if (len > 0 && s[len-1] == '\n') s[--len] = '\0'; // 移除換行
char max_c = s[0];
for (int i = 1; i < len; i++)
if (s[i] > max_c) max_c = s[i];
for (int i = 0; i < len; i++) {
putchar(s[i]);
if (s[i] == max_c) printf("(max)");
}
putchar('\n');
}
return 0;
}
輸入: abcdefgfedcba 最大字元: g (ASCII 103) 輸出: abcdefg(max)fedcba ✓ 輸入: xxxxx 最大字元: x 輸出: x(max)x(max)x(max)x(max)x(max) ✓
fgets 讀取含換行,需手動移除A-Z (65-90) a-z (97-122),小寫較大