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.
- Split the input string into an array of words.
- Create a
StringBuilder
to build the resulting string. - Loop through each word in the array using its index.
- For each word, check if its index is even.
- If the index is even, convert the word to uppercase and append it to the result string.
- If the index is odd, append the word as is.
- Return the final string after processing all words.
Oplossing
solution.java
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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?
Bedankt voor je feedback!
Sectie 3. Hoofdstuk 3
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.