查找字符串中最大字符 请问一下为什么只能输出第一位字符

请问一下为什么只能输出第一位字符
是哪里出错了

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;
}

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632