题目:对于给定的字符串,找出该字符串中Ascii码值最大的那个字符,
并用输入的单个字符替换掉该最大字符,然后输出;
如果有多个最大字符,则全部替换
样例:
请输入要替换的字符:H
替换后的字符串为I am a stHdent of hHst
请按任意键继续. . .
代码如下:
#include<iostream>
using namespace std;
int main()
{
char str[]="I am a student of hust";
char max,c;
cout<<"请输入要替换的字符:";
cin>>c;
/**********Program**********/
max=str[0];
for(int i=0;str[i]!='\0';i++)
{
if(max<str[i])
max=str[i];
}
for(int i=0;str[i]!=0;i++)
{
if(str[i]==max)
str[i]=c;
}
/********** End **********/
cout<<"替换后的字符串为"<<str<<endl;
return 0;
}