Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge: Uppercase Even Words | String Advanced
Java Extended

book
Challenge: Uppercase Even Words

Tarea

Swipe to start coding

Your task is to convert each word at an even position to uppercase and return the modified string.

  1. Split the input string into an array of words.
  2. Create a StringBuilder to build the resulting string.
  3. Loop through each word in the array using its index.
  4. For each word, check if its index is even.
  5. If the index is even, convert the word to uppercase and append it to the result string.
  6. If the index is odd, append the word as is.
  7. Return the final string after processing all words.

Solución

java

solution

package com.example;

public class Main {
static String makeEvenWordsUppercase(String sentence) {
String[] array = sentence.split(" ");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if (i % 2 == 0) {
builder.append(array[i].toUpperCase()).append(" ");
} else {
builder.append(array[i]).append(" ");
}
}
return builder.toString();
}

// do not modify the code below
public static void main(String[] args) {
String sentence = "Green hot-dog with cucumber and mustard";
String result = makeEvenWordsUppercase(sentence);
System.out.println(result);
}
// do not modify the code above
}

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3
package com.example;

public class Main {
static String makeEvenWordsUppercase(String sentence) {
String[] array = ___.split("___");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < ___.length; i++) {
if (i % ___ == 0) {
builder.append(___[i].___).append(" ");
} else {
builder.append(___[i]).append(" ");
}
}
return ___.toString();
}

// do not modify the code below
public static void main(String[] args) {
String sentence = "Green hot-dog with cucumber and mustard";
String result = makeEvenWordsUppercase(sentence);
System.out.println(result);
}
// do not modify the code above
}
toggle bottom row
some-alt