第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在字符串中查找最長的單詞,即偶數(shù)

如何在字符串中查找最長的單詞,即偶數(shù)

Qyouu 2022-09-28 10:26:14
我需要在字符串中找到一個(gè)既長又均勻的單詞。舉幾個(gè)例子:這句話.這應(yīng)該返回字符串,因?yàn)樗亲畲箝L度的單詞,長度為 ,而不是因?yàn)闃?gòu)造是奇數(shù)的長度。Time to construct great arttimeeven4construct9另外,在 的示例中。這應(yīng)該返回字符串 ,因?yàn)樗桥紨?shù),并且偶數(shù)長度為 。它不會(huì)返回單詞,因?yàn)槭紫瘸霈F(xiàn)。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);我的代碼成功地打印了最長的單詞,但是沒有考慮最長的字符串長度是否為偶數(shù),以及它是否首先出現(xiàn)。我嘗試過在我的for循環(huán)中使用一些模數(shù)字符,但它并沒有跟蹤最偉大的單詞是否甚至移動(dòng)到下一個(gè)最大的單詞。另外,我很難跟蹤這個(gè)詞是否先出現(xiàn)。我試圖解釋甚至: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];                }            }        }
查看完整描述

4 回答

?
RISEBY

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以使用模運(yùn)算符 () 來檢查字符串長度是否為偶數(shù):%


string.length() % 2 == 0

為此,您可以使用 Arrays.stream() 來查找長度為偶數(shù)的最長字符串:


String longestWord = Arrays.stream(sentence.split(" ")) // creates the stream with words

        .filter(s -> s.length() % 2 == 0) // filters only the even length strings

        .max(Comparator.comparingInt(String::length)) // finds the string with the max length

        .orElse(" "); // returns " " if none string is found


查看完整回答
反對(duì) 回復(fù) 2022-09-28
?
www說

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

更改是您比較最長長度的>,而不是>=并檢查長度是否均勻。


要適應(yīng)存在其他符號(hào)(如 '.')的情況,請使用正則表達(dá)式模式。


public static void main(String[] args) {

    // TODO Auto-generated method stub


    String input = "I am good.";

    String[] input_words = input.split(" ");

    String longestWord = " ";


    for(String word : input_words) {

        Pattern p = Pattern.compile("^[a-zA-Z]+");

        Matcher m = p.matcher(word);

        m.find();

        if(m.group().length() % 2 == 0 && m.group().length() > longestWord.length()) {

            longestWord = m.group();

        }

    }

    System.out.println("result : " + longestWord);

}

這將打印出最大的第一個(gè)偶數(shù)字


查看完整回答
反對(duì) 回復(fù) 2022-09-28
?
桃花長相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

在python中這個(gè)問題的解決方案是:


def longestevenword(sentence):

  #converting the sentence into lists

  string_list = sentence.split()

  size = 0

  #iteration through each word in the list

  for word in string_list:

    #check to see if the word has even number of characters

    if len(word)%2 == 0:

      #checking if the length of the of the word

      if size <= len(word):

        size = len(word)

        even_word = word

    else:

      continue        

  # printing the longest even word in a given sentence.

  print(even_word)



## function call to test

longestevenword("This is a cat that had a puppyy")


Output: puppyy

它也可以在我的GitHub https://github.com/jaikishpai/CommonSolutions/blob/main/LongestEvenWord.py


查看完整回答
反對(duì) 回復(fù) 2022-09-28
?
jeck貓

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊

public class LongestEvenString {

    public static void main(String[] args) {

        String test = "this is a sample input for the problem";

        System.out.println(longestEvenString(test));

    }


    public static String longestEvenString(String test) {

//        Splits a sentence to array of Strings.

        String[] words = test.split(" ");

//        curlen stores length of current word in string Array

//        maxlen stores length of maximum_length word in string Array

        int curlen = 0;

        int maxlen = 0;

        String output = "";

        for (String word:words) {

            curlen = word.length();

//            loop runs only if length of the current word is more than

//            maximum_length thus excluding future same maximum_length words.

//            and also checks for even_length of that word

            if(curlen > maxlen && curlen%2 == 0){

                maxlen = curlen;

                output = word;

            }

        }

//        returns maximum_even_length word which occurred first

        return output;

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-09-28
  • 4 回答
  • 0 關(guān)注
  • 119 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)