Reverse a String and convert it to lower case in Java

Photo by nousnou iwasaki on Unsplash

To install Java( I have installed the JDK 8 here)

Java SE Development Kit 8 — Downloads — Oracle

Choose the macOS X64 option and then click the download that corresponds to it. After that you have to choose the license agreement option and then oracle will ask you to create an account and in case you do not have any then create an account. After you sign in with your account then only the download would start.

I have used Eclipse IDE for writing my Java programs.

To install Eclipse.

download an Eclipse IDE package

Then clicked on the link below.

Eclipse IDE for Java Developers

and downloaded the macOS version.

There are many ways of solving this problem.

The first way is taking a declaring an empty character array and then giving it a length of the string variable. Then looping through the empty array and then assigning the character at the place backward from the string variable to the character array. Then take the reverse character array variable and get the equivalent string value of the array and then convert it to lowercase.

Using StringBuffer()

StringBuffer is a peer class of String. String class is fixed-length and immutable. To any modifications to the String, StringBuffer and StringBuilder are used. However both of the StringBuffer and StringBuilder classes are very similar but StringBuffer is synchronized whereas StringBuffer is synchronous. StringBuilder is more faster than StringBuffer but for thread safety, I am using StringBuffer.

First create we a StringBuffer object . We can do two things here, either we can either pass the string variable inside the StringBuffer() and then call the reverse() method on the StringBuffer object or we can loop through the string variable and then append the (character at the string variable) to the StringBuffer object in a backward way I.e first append the last character of the string variable, then the second last, until the first element of the string variable. Then convert the StringBuffer object to a string object and convert it to lower case.

Resources

Flatiron School Alumni | Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store