Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
salida de una oración de la palabra más corta a la más larga

Esto es lo que actualmente he estado tratando de obtener un resultado como este:

 input: this is a sentence output: a is this sentence
 import java.util.Scanner; Scanner input = new Scanner(System.in); System.out.print("Enter a sentance:"); String text = input.nextLine(); String[] words = text.split(""); String temp; for (int i = 0; i < words.length; i++) { for (int j = 0; j < (words.length - 1); j++) { if (words[j].compareTo(words[j + 1]) < 0) { temp = words[j]; words[j] = words[j + 1]; words[j + 1] = temp; } } System.out.print(words[i]); } } }

¡Si alguien pudiera ayudar, sería genial!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede convertir la entrada en una lista y luego ordenar usando una lambda:

 Scanner input = new Scanner(System.in); System.out.print("Enter a sentence:"); String text = input.nextLine(); List<String> words = Arrays.asList(text.split(" ")); words.sort(Comparator.comparingInt(String::length)); System.out.println(String.join(" ", words));

Esto imprime (para la entrada this is a sentence ):

 a is this sentence
over 4 years ago · Santiago Trujillo Report

0

@Test public void testSort() { Assertions.assertEquals("a is this sentence", sort("this is a sentence")); } private String sort(String sentence) { return Arrays.stream(sentence.split(" ")) .sorted(Comparator.comparingInt(String::length)) .collect(Collectors.joining(" ")); }

// dividir palabras por espacio, luego ordenar por longitud y unir palabras nuevamente por espacio.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!