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

book
Challenge: Uppercase Even Words

Taak

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.

Oplossing

solution.java

solution.java

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
}

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 3
single

single

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
}

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

We use cookies to make your experience better!
some-alt