how wud i create a code tht wud display letters of a string entered by user according to the number being odd or even?

so say user enters sentence:

i like ice cream.

the even output : ilkiecem
odd output: the rest of the letters (tht are at odd positions)

i will highly appreciate if anyone can provide me some explanation thru some code...

8 Years
Discussion Span
Last Post by BestJewSinceJC

Hi testing321 and welcome to DaniWeb :)

Hint 1: You can make use of certain String functions to work out the characters in the String, such as charAt(). Here is a link to the charAt function's documentation.

Hint 2: You can tell if a number is odd or even through the use of modulo arithmetic. For example, 5 % 2 = 1, which means it is odd. 8 % 2 = 0 which means it is even.

Hope this gives you a start. Try coding some yourself, post back with your code if you get stuck with specific questions.


Use the String class's toCharArray method, then loop through the character array using a for loop, only printing out every even position or odd position. You can print out even positions or odd positions by using modulus. What modulus does is it returns the remainder of a division. So an example of modulus would be 4 % 2 = 0 and 6 % 4 = 2 (the % sign is modulus).

Good luck!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.