对于输入的字符串(全由小写字母组成,长度小于100),查找其中的ASCII码最大字母,在该字母后面插入字符串“(max)”。 输出的字符串后面带空行。
【输入格式】
一行一个字符串
【输出格式】
按要求输出一行一个字符串
【输入样例】
abcdefgfedcba
【输出样例】
abcdefg(max)fedcba
#include <stdio.h>
char find_max_letter(char *s)
{
char m = *s;
while (*s)
{
if (*s > m)
m = *s;
s++;
}
return m;
}
int main()
{
char a[100], b[500], *s = "(max)";
while (scanf("%s", a) == 1)
{
char max = find_max_letter(a);
const char *p = a;
char *q = b;
while (*p)
{
*q++ = *p;
if (*p == max)
{
const char *t = s;
while (*t)
*q++ = *t++;
}
p++;
}
*q = '\0';
printf("%s\n", b);
}
return 0;
}
可能有bug,有的话还请告知
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
int i;
string str, s = "(max)";
char max = 'a';
cin >> str;
for (i = 0; i < str.length(); i++)
{
if (str[i] > max)
{
max = str[i];
}
}
for (i = 0; i < str.length(); i++)
{
if (str[i] == max)
{
str.insert(i+1, s, 0, 5);
i += 5;
}
}
cout << str << endl;
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char t[105];
gets(t);
int i,len=strlen(t),p=0;
char max=t[0];
for( i=1; i< len; i++)
{
if(max<t[i])
{
max=t[i];
p=i;
}
}
for( i=0; i<= p; i++)
printf("%c",t[i]);
printf("%c%c%c%c%c",'(','m','a','x',')');
for( i=p+1; i< len; i++)
printf("%c",t[i]);
printf("\n");
return 0;
}