如何在字符串中查找最长的单词,即偶数

我需要在字符串中找到一个既长又均匀的单词。举几个例子:


这句话.这应该返回字符串,因为它是最大长度的单词,长度为 ,而不是因为构造是奇数的长度。Time to construct great arttimeeven4construct9


另外,在 的示例中。这应该返回字符串 ,因为它是偶数,并且偶数长度为 。它不会返回单词,因为首先出现。Time to write great codeTime4codeTime


        String[] array = sentence.split(" ");

        String longestWord = " ";


        for (int i = 0; i < array.length; i ++) {

            if (array[i].length() >= longestWord.length()) {

                longestWord = array[i];

            }

        }


        System.out.println(longestWord);

我的代码成功地打印了最长的单词,但是没有考虑最长的字符串长度是否为偶数,以及它是否首先出现。


我尝试过在我的for循环中使用一些模数字符,但它并没有跟踪最伟大的单词是否甚至移动到下一个最大的单词。


另外,我很难跟踪这个词是否先出现。


我试图解释甚至:


for (int i = 0; i < array.length; i ++) {

            if (array[i].length() >= longestWord.length()) {

                longestWord = array[i];

                if (longestWord.length() % 2 != 0) {

                    break;

                }

                else {

                    longestWord = array[i];

                }

            }

        }


Qyouu
浏览 93回答 4
4回答

RISEBY

您可以使用模运算符 () 来检查字符串长度是否为偶数:%string.length() % 2 == 0为此,您可以使用 Arrays.stream() 来查找长度为偶数的最长字符串:String longestWord = Arrays.stream(sentence.split(" ")) // creates the stream with words&nbsp; &nbsp; &nbsp; &nbsp; .filter(s -> s.length() % 2 == 0) // filters only the even length strings&nbsp; &nbsp; &nbsp; &nbsp; .max(Comparator.comparingInt(String::length)) // finds the string with the max length&nbsp; &nbsp; &nbsp; &nbsp; .orElse(" "); // returns " " if none string is found

www说

更改是您比较最长长度的>,而不是>=并检查长度是否均匀。要适应存在其他符号(如 '.')的情况,请使用正则表达式模式。public static void main(String[] args) {&nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; String input = "I am good.";&nbsp; &nbsp; String[] input_words = input.split(" ");&nbsp; &nbsp; String longestWord = " ";&nbsp; &nbsp; for(String word : input_words) {&nbsp; &nbsp; &nbsp; &nbsp; Pattern p = Pattern.compile("^[a-zA-Z]+");&nbsp; &nbsp; &nbsp; &nbsp; Matcher m = p.matcher(word);&nbsp; &nbsp; &nbsp; &nbsp; m.find();&nbsp; &nbsp; &nbsp; &nbsp; if(m.group().length() % 2 == 0 && m.group().length() > longestWord.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; longestWord = m.group();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("result : " + longestWord);}这将打印出最大的第一个偶数字

桃花长相依

在python中这个问题的解决方案是:def longestevenword(sentence):&nbsp; #converting the sentence into lists&nbsp; string_list = sentence.split()&nbsp; size = 0&nbsp; #iteration through each word in the list&nbsp; for word in string_list:&nbsp; &nbsp; #check to see if the word has even number of characters&nbsp; &nbsp; if len(word)%2 == 0:&nbsp; &nbsp; &nbsp; #checking if the length of the of the word&nbsp; &nbsp; &nbsp; if size <= len(word):&nbsp; &nbsp; &nbsp; &nbsp; size = len(word)&nbsp; &nbsp; &nbsp; &nbsp; even_word = word&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; # printing the longest even word in a given sentence.&nbsp; print(even_word)## function call to testlongestevenword("This is a cat that had a puppyy")Output: puppyy它也可以在我的GitHub https://github.com/jaikishpai/CommonSolutions/blob/main/LongestEvenWord.py

jeck猫

public class LongestEvenString {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String test = "this is a sample input for the problem";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(longestEvenString(test));&nbsp; &nbsp; }&nbsp; &nbsp; public static String longestEvenString(String test) {//&nbsp; &nbsp; &nbsp; &nbsp; Splits a sentence to array of Strings.&nbsp; &nbsp; &nbsp; &nbsp; String[] words = test.split(" ");//&nbsp; &nbsp; &nbsp; &nbsp; curlen stores length of current word in string Array//&nbsp; &nbsp; &nbsp; &nbsp; maxlen stores length of maximum_length word in string Array&nbsp; &nbsp; &nbsp; &nbsp; int curlen = 0;&nbsp; &nbsp; &nbsp; &nbsp; int maxlen = 0;&nbsp; &nbsp; &nbsp; &nbsp; String output = "";&nbsp; &nbsp; &nbsp; &nbsp; for (String word:words) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curlen = word.length();//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loop runs only if length of the current word is more than//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maximum_length thus excluding future same maximum_length words.//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and also checks for even_length of that word&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(curlen > maxlen && curlen%2 == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxlen = curlen;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output = word;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }//&nbsp; &nbsp; &nbsp; &nbsp; returns maximum_even_length word which occurred first&nbsp; &nbsp; &nbsp; &nbsp; return output;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java