42.6k views
3 votes
In this exercise, you are given a word or phrase. If the length of that word or phrase is odd, add a space to the end and return it. If it is even, just return it as is.

Example:

makeEven("Hi") --> "Hi"
makeEven("odd") --> "odd "

This is in Java.

1 Answer

1 vote

Answer:

Answer is in the provided screenshot!

Step-by-step explanation:

I have modified my usual print command to wrap quotation marks around its output - just for visibility.

My statement:

input.length() % 2 == 0

Simply means, if the remainder of the length of the string divided by 2 is 0. Or in other terms, if the string length is even.

You may not have encountered ternary operators yet, but I utilise one of these to make the code as compact as possible.

The way that they work is that you provide them with a boolean value to evaluate, and they return the object after the ? if the value is true, or the object after the : if the value is false.

So all together, if the String's length is even output the String, else output the string with an additional space.

In this exercise, you are given a word or phrase. If the length of that word or phrase-example-1
User Seveninstl
by
4.2k points