String Reverse the Java Way!

I have published an ebook. A compilation of 100 Java(Interview)Programming problems which have been solved . I have given clear explanation and the code in the book. Believe me when I say, this will kick start you to achieve the job at your dream company.

Click on this link to get you to the landing page. It is completely free when you use kindle amazon. Take a look at it.

Given a string (array of character), reverse the string

Input String: “Mike”

Output: “ekiM”

Input String: “break”

Output: “kaerb”

Code:

public class Answer implements QuestionInterface {

@Override

public String func(String str)

{

String b;

if(str==null)

return null;

else

{

StringBuffer c=new StringBuffer(str);

c.reverse();

b=c.toString();

}

return b;

}

}

Explanation:

The problem is to reverse a given string.

Since the strings class in java is immutable the string buffer class is used that makes the string value mutable.

The string buffer class has the reverse() function is used to reverse the string.

  • The input string is passed to the function func(String str)
  • If the string input is empty then null value is returned as output.
  • The string is stored into a string buffer class to make it mutable.
  • Then the reverse() function is used to reverse the input value that is stored in a string buffer class.
  • Then the output is coverted to string using tostring() function and returned as output.

If you are a beginner to java. Here is an outstanding book to get you started with Java basics and you can Learn it in Just 1 Day!

M.A Ohen

This is a concise, well written, and well organized book. I didn’t even realize I was ‘learning’ until I was coding a project and got it correct without double checking punctuation, spelling, capitalization, etc.

It is Completely free if you have Kindle Unlimited plan. It is worth the read if you want to learn java on your mobile and if you can’t take a paperback to each and every place you travel to.

Author: Architha Harinath

You can follow House of Codes to receive updates when we post new coding challenges. Also you could send us your solutions or your ideas for any coding challenge. We would be thrilled to read them. ;)

Code -> Understand-> Repeat is my motto. I am a Data Engineer who writes about everything related to Data Science and Interview Preparation for SDE.

Code -> Understand-> Repeat is my motto. I am a Data Engineer who writes about everything related to Data Science and Interview Preparation for SDE.