字符串找出最长字符和他的长度 并输出这个字符

img


没有思路 可以给点思路吗 写了一个但是不对 大家帮忙看看 感谢了 头秃


#include<iostream>
using namespace std;
void split(char *str)
{
    int num_word=0;
    int temp=0,maxlen=0,maxwhere=0;
    for(int i=0;str[i]!='\0';i++)
    {
        if(str[i+1]=='\0'&&str[i]>='a'&&str[i]<='z') str[i+1]=' ';
        if(str[i]==' ')
        {
            num_word++;
            if(temp>maxlen)
            {
                maxlen=temp;
                maxwhere=i-temp;
                temp=0;
            }
            else temp=0;
        }
        else
        {
            temp++;
        }
    }
    cout << num_word << " " << maxlen << " ";
    while(str[maxwhere]!=' ') cout << str[maxwhere++];

}
int main()
{
    char str[500];
    cin.getline(str,500);
    split(str);
    return 0;
}