方法1:通过设置一个数组,来存入最大字符的下标,t[101],设置一个count来判断最大字符的个数
分两种情况;1、只有一个最大字符,存入在t[0]位置;
2、如果有多个字符,在t[count++]中存入相同最大字符的下标;
如果已经找到多个最大字符,但后面有更大的,在1的判断中,对count进行初始化=1;
`#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
string s;
int t[101]={0};
while(cin>>s)
{
int max=0;
t[0]=0;
int count=1;
for(int i=1;i<s.size();i++)
{
if(s[i]>s[max])
{
count=1;
t[0]=i;
max=i;
}
else if(s[i]==s[max])
{
t[count++]=i;
max=i;
}
else
continue;
}
if(count==1)
{
s.insert(t[0]+1,"(max)");
}
else
{
for(int i=0;i<count;i++)
{
s.insert(t[i]+1+i*5,"(max)");
}
}
cout<<s<<endl;
}
}`
指针
在这里插入代码片
#include
#include
#include
using namespace std;
int main()
{
string s;
while(cin>>s)
{
int max=s[0];
const char *t=s.c_str();
while(*t)
{
if(*t>max)
{
max=*t;
}
t++;
}
t=s.c_str();
while(*t)
{
if(*t==max)
cout<<*t<<"(max)";
else
cout<<*t;
*t++;
}
cout<<endl;
}
}