请问一下为什么只能输出第一位字符
是哪里出错了
using namespace std;
#include<iostream>
#include<string.h>
char *maxc(char *s,int n)
{
// 在此处补充你的代码
char max;
max=s[0];
for(int i=0;i<n;i++){
if (max<=s[i]){max=s[i];
}
}
return s;
}int main()
{
char str[]="This is a string.";
cout<<*maxc(str,strlen(str))<<endl; //输出t
cout<<(++*maxc(str,strlen(str)))<<endl; //输出u
return 0;
}
maxc() 函数应该返回 max, 不是返回s
你题目的解答代码如下:
using namespace std;
#include <iostream>
#include <string.h>
char maxc(char *s, int n)
{
// 在此处补充你的代码
char max;
max = s[0];
for (int i = 0; i < n; i++)
{
if (max <= s[i])
{
max = s[i];
}
}
return max; //返回max
}
int main()
{
char str[] = "This is a string.";
cout << maxc(str, strlen(str)) << endl; //输出t
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!