找最长单词,输入平台的输入样例无法结束程序

代码应该没有错误
图点开比较清晰

  • 题目

img

  • 代码
void find(char b[81])
{

    int i;
    int o;
    int p;
    int count = 0;
    int length = 0;
    int start = 0;
    int m;
    int maxlength = 0;
    int dot = 0;
    for (o = 0; b[o] != '\0'; o++)
    {
        if (b[o] == ' ')
            start = o + 1;
        if (o == start)
        {
            m = o;
            while ((b[o] >= 'A' && b[o] <= 'Z') || (b[o] >= 'a' && b[o] <= 'z'))
            {
                o++;
                length++;
            }
            if (maxlength < length)
            {
                maxlength = length;
                dot = start;
            }
            length = 0;
            o = m;
        }
        if (b[o + 1] == '\0')
        {
            for (i = dot; ((b[i] >= 'A' && b[i] <= 'Z') || (b[i] >= 'a' && b[i] <= 'z')); i++)
            {
                printf("%c", b[i]);
            }
            printf("\n");
        }
    }
}
int main()
{

    char a[100];
    while(gets(a))
    {
        find(a);
    }
    return 0;
}

  • 运行效果

img

如果觉得可以,还请 采纳:

using System;
using System.Linq;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                string input = Console.ReadLine();
                if (input == null) break;

                string[] words = input.Split(' ');
                string maxLengthWord = words
                    .Where(word => word.All(char.IsLetter))
                    .OrderByDescending(word => word.Length)
                    .ThenBy(word => words.ToList().IndexOf(word))
                    .First();

                Console.WriteLine(maxLengthWord);
            }
        }
    }
}

1、使用while循环读入字符串,如果读入的字符串为空,则退出循环。
2、使用Split方法将读入的字符串分割成数组,存入words数组。
3、使用Where方法过滤出字符串数组中全部由字母组成的单词。
4、使用OrderByDescending方法按单词长度从大到小排序,使用ThenBy方法再按出现的先后顺序排序。
5、使用First方法取出排序后的第一个元素,即最长的单词。
6、输出最长单词。

可以在while内第一行就加一个判断,判断这次的输入a是不是第一个字符就是回车\n,是的话就break跳出循环停止。