您好,欢迎来到星星旅游。
搜索
您的当前位置:首页最长单词

最长单词

来源:星星旅游


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

一个以’.'结尾的简单英文句子,单词之间用空格分隔,没有缩写形式和其它特殊形式,求句子中的最长单词。

输入

一个以’.'结尾的简单英文句子(长度不超过500),单词之间用空格分隔,没有缩写形式和其它特殊形式。

输出

该句子中最长的单词。如果多于一个,则输出第一个。

样例输入

I am a student of Peking University.

样例输出

University

C语言代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    char sentence[501];
    fgets(sentence, 501, stdin); // 读取输入的英文句子
    sentence[strcspn(sentence, “\n”)] = ‘\0’; // 去除换行符

    char *token = strtok(sentence, " “); // 分割句子获取第一个单词
    char *longestWord = token;
    while (token!= NULL) {
        if (strlen(token) > strlen(longestWord)) { // 比较单词长度
            longestWord = token;
        }
        token = strtok(NULL, " “); // 获取下一个单词
    }
    printf(”%s\n”, longestWord);
    return 0;
}

C++ 代码

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
    string sentence;
    getline(cin, sentence); // 读取输入的英文句子

    stringstream ss(sentence);
    string word, longestWord;
    ss >> longestWord; // 获取第一个单词
    while (ss >> word) {
        if (word.length() > longestWord.length()) { // 比较单词长度
            longestWord = word;
        }
        }
    cout << longestWord << endl;
    return 0;
}

Java代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String sentence = scanner.nextLine(); // 读取输入的英文句子

        String[] words = sentence.split(" ");
        String longestWord = words[0];
        for (int i = 1; i < words.length; i++) {
            if (words[i].length() > longestWord.length()) { // 比较单词长度
                longestWord = words[i];
            }
        }
        System.out.println(longestWord);
    }
}

Python代码

sentence = input()[:-1] # 读取输入的英文句子并去掉末尾的.
words = sentence.split(" ") # 分割句子得到单词列表
longest_word = words[0]
for word in words[1:]:
    if len(word) > len(longest_word): # 比较单词长度
        longest_word = word
print(longest_word)


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

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- stra.cn 版权所有 赣ICP备2024042791号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务