创建c++源程序test3.cpp:程序功能:利用指针在字符串str中找到字符ASCⅡ码值最大的字符。

来源:百度知道 编辑:UC知道 时间:2024/05/31 04:29:04
创建c++源程序test3.cpp:程序功能:利用指针在字符串str中找到字符ASCⅡ码值最大的字符。
如:运行结果如下:
请输入一字符串:
I compile this program by C++
该字符串中最大的字符是:y
要求:
(1)定义字符数组str表示字符串;
(2)输入字符串;
(3)用循环语句实现求字符串最大值。
有没更简单点的

我给你做,等会
答案如下:
#include<iostream>
#include<string>
using namespace std;

int main()
{
char s[100],*ps,c;
cout<<"请输入一字符串:"<<endl;
gets(s);
ps=s;

c=*ps++;
while(*ps!='\0')
{
if(*ps>c&&isalpha(*ps))
c=*ps;
ps++;
}
cout<<"该字符串中最大的字符是:"<<c<<endl;
return 1;
}

验证通过!

#include <iostream>
using namespace std;

char MaxChar(char str[])
{
char * p=str;
char ans=0;
while(* p!=0)
{
if(* p>ans)
ans=* p;
p++;
}
return ans;
}

int main()
{
char str[1000];
cout<<"请输入一字符串:"<<endl;
cin.getline(str,sizeof(str));
cout<<MaxChar(str)<<endl;
return 0;
}