最长最短单词

最长最短单词

      • C语言实现
      • C++实现
      • Java实现
      • Python实现

|-----------------------------|
| 💐The Begin💐点点关注,收藏不迷路💐 |

输入1行句子(不多于200个单词,每个单词长度不超过100),只包含字母、空格和逗号。单词由至少一个连续的字母构成,空格和逗号都是单词间的间隔。

试输出第1个最长的单词和第1个最短单词。

输入

一行句子。

输出

第1行,第一个最长的单词。

第2行,第一个最短的单词。

样例输入

c 复制代码
I am studying Programming language C in Peking University

样例输出

c 复制代码
Programming
I

C语言实现

c 复制代码
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_LENGTH 200 * 100  // 考虑句子最多200个单词,每个单词最长100字符,预留足够空间

int main() {
    char sentence[MAX_LENGTH];  // 存储输入的句子
    fgets(sentence, MAX_LENGTH, stdin);  // 从标准输入读取句子,包含换行符

    int len = strlen(sentence);
    if (len > 0 && sentence[len - 1] == '\n') {  // 去除换行符
        sentence[len - 1] = '\0';
    }

    char longest_word[100];  // 存储最长的单词
    char shortest_word[100];  // 存储最短的单词
    strcpy(longest_word, "");  // 初始化为空字符串
    strcpy(shortest_word, "");  // 初始化为空字符串

    int current_word_start = 0;  // 当前单词开始的位置
    int current_word_length = 0;  // 当前单词的长度
    int max_length = 0;  // 记录最长单词长度
    int min_length = 100;  // 初始设为一个较大值,方便比较找最短单词

    for (int i = 0; i < len; i++) {
        if (isalpha(sentence[i])) {  // 如果是字母,说明在单词内
            current_word_length++;
        } else if (sentence[i] =='' || sentence[i] == ',') {  // 如果是间隔符号,说明单词结束
            if (current_word_length > max_length) {  // 判断是否是目前最长的单词
                max_length = current_word_length;
                strncpy(longest_word, sentence + current_word_start, current_word_length);
                longest_word[current_word_length] = '\0';
            }
            if (current_word_length < min_length && current_word_length > 0) {  // 判断是否是目前最短的单词且长度大于0
                min_length = current_word_length;
                strncpy(shortest_word, sentence + current_word_start, current_word_length);
                shortest_word[current_word_length] = '\0';
            }
            current_word_start = i + 1;  // 更新下一个单词开始的位置
            current_word_length = 0;  // 重置当前单词长度
        }
    }

    // 处理最后一个单词的情况(因为循环结束时没处理最后一个单词结束的情况)
    if (current_word_length > max_length) {
        max_length = current_word_length;
        strncpy(longest_word, sentence + current_word_start, current_word_length);
        longest_word[current_word_length] = '\0';
    }
    if (current_word_length < min_length && current_word_length > 0) {
        min_length = current_word_length;
        strncpy(shortest_word, sentence + current_word_start, current_word_length);
        shortest_word[current_word_length] = '\0';
    }

    printf("%s\n", longest_word);
    printf("%s\n", shortest_word);

    return 0;
}

C++实现

cpp 复制代码
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;

int main() {
    string sentence;
    getline(cin, sentence);  // 从标准输入读取句子,自动去除换行符

    string longest_word;  // 存储最长的单词
    string shortest_word;  // 存储最短的单词
    longest_word = "";  // 初始化为空字符串
    shortest_word = "";  // 初始化为空字符串

    stringstream ss(sentence);  // 创建stringstream对象,用于分割句子
    string current_word;
    int max_length = 0;  // 记录最长单词长度
    int min_length = 100;  // 初始设为一个较大值,方便比较找最短单词

    while (ss >> current_word) {  // 从stringstream中按空格或逗号分割读取单词
        int length = current_word.length();
        if (length > max_length) {  // 判断是否是目前最长的单词
            max_length = length;
            longest_word = current_word;
        }
        if (length < min_length && length > 0) {  // 判断是否是目前最短的单词且长度大于0
            min_length = length;
            shortest_word = current_word;
        }
    }

    cout << longest_word << endl;
    cout << shortest_word << endl;

    return 0;
}

Java实现

java 复制代码
import java.util.Scanner;

public class LongestAndShortestWord {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String sentence = scanner.nextLine();  // 从标准输入读取句子

        String longestWord = "";  // 存储最长的单词
        String shortestWord = "";  // 存储最短的单词
        int maxLength = 0;  // 记录最长单词长度
        int minLength = 100;  // 初始设为一个较大值,方便比较找最短单词

        String[] words = sentence.split("[,\\s]+");  // 按空格或逗号分割句子得到单词数组

        for (String word : words) {
            int length = word.length();
            if (length > maxLength) {  // 判断是否是目前最长的单词
                maxLength = length;
                longestWord = word;
            }
            if (length < minLength && length > 0) {  // 判断是否是目前最短的单词且长度大于0
                minLength = length;
                shortestWord = word;
            }
        }

        System.out.println(longestWord);
        System.out.println(shortestWord);

        scanner.close();
    }
}

Python实现

python 复制代码
sentence = input()  # 从标准输入读取句子

words = sentence.split()  # 以空格为分隔符将句子分割成单词列表

longest_word = ""  # 存储最长的单词
shortest_word = ""  # 存储最短的单词

for word in words:
    word = word.strip(",")  # 去除单词两端可能存在的逗号
    length = len(word)
    if length > len(longest_word):  # 判断是否是目前最长的单词
        longest_word = word
    if length < len(shortest_word) or shortest_word == "":  # 判断是否是目前最短的单词
        shortest_word = word

print(longest_word)
print(shortest_word)

|---------------------------|
| 💐The End💐点点关注,收藏不迷路💐 |

相关推荐
IronMurphy2 小时前
【算法四十三】279. 完全平方数
算法
lee_curry2 小时前
第四章 jvm中的垃圾回收器
java·jvm·垃圾收集器
墨染天姬2 小时前
【AI】Hermes的GEPA算法
人工智能·算法
papership2 小时前
【入门级-数据结构-3、特殊树:完全二叉树的数组表示法】
数据结构·算法·链表
weixin_520649872 小时前
WinForm数据展示组件ListView
c#
smj2302_796826522 小时前
解决leetcode第3911题.移除子数组元素后第k小偶数
数据结构·python·算法·leetcode
山甫aa3 小时前
差分数组 ----- 从零开始的数据结构
数据结构
早日退休!!!3 小时前
《数据结构选型指南》笔记
数据结构·数据库·oracle
九转成圣3 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
Beginner x_u3 小时前
链表专题:JS 实现原理与高频算法题总结
javascript·算法·链表