m0_68422617 2022-04-05 17:54 采纳率: 77.8%
浏览 35
已结题

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

img


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

  • 写回答

1条回答 默认 最新

  • bostonAlen 2022-04-05 18:16
    关注
    
    #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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月14日
  • 已采纳回答 4月6日
  • 创建了问题 4月5日