java - Reversing string and toggle lower-upper-case -
i need write application takes string input. after processing, should print out string reversed, , characters switched lower uppercase , vise versa. want achieve using stringbuilder
.
sample run:
input: hello human
output: namuh olleh
here straight forward , simple way of solving problem:
read user input using instance
scanner
class. ,nextline()
method. (constructscanner
givingsystem.in
argument.)create
stringbuilder
.fetch array of characters input string, using instance
input.tochararray()
reverse array
arrays.aslist
,collections.reverse
collections.reverse(arrays.aslist(yourchararray));
loop through list using instance for-each loop.
for (character c : yourchararray) { ... }
for each character, check if
c.isuppercase
(if appendc.tolowercase()
) else appendc.touppercase()
.
(an alternative approach give stringbuilder input-string argument, , manipulate string in-place using reverse
, charat
, setcharat
methods.)
Comments
Post a Comment