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

book
Challenge: Uppercase Even Words

Opgave

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.

Løsning

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
}

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 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
}

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt