Topics Index

Print reverse of the string

If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to print the reverse of given input string. Use System.out.println or System.out.print for printing.

Input (Integer) Printed Output
queue eueuq
gangnam mangnag
malayalam malayalam

class PrintReverse
    public static void main(String s[])


    public static void printReverse(String input)

Topic: charAt() Method In Java - Java Character Extraction

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Execution will start from main method compiler will go to first line of main method  that is printReverse("gangnam") that means it will call that perticular method name with "gangnam" string as a parameter of that method.... Inside that method printReverse we have to take one string variable to store the reverse string... say String "reverse"  this is the variable name and initialize the variable with space ("").....  then store the length of the given Stirng gangnam in one variable say integer 'i' here  the length of that variable is 7 as given in this program...... after that we have to write one for-loop to reverse the string with initial value from string's "length-1" to 0 and concatanate the string reverse (Using '+' operator) ..with letters in the original string "gangana" stored in variable input with the statement                                                            " reverse = reverse + input.charAt(i)" ... this single statement will be present in the      for-loop so as I told earlier initially i value is length-1 i.e i=6 so input.charAt(6) means the last letter of original string will be concatenate with the string variable reverse like this till 0 th index loop will execute.... finally we can print the reverse variable... from this we can get reverse string output

Posted by Bhagi Bhagyasri    2015-01-12 08:21:00

singlw word will make it if u use strrev() function i.e strrev(str);

Posted by Sai Veerendra    2015-01-12 08:32:57

my hint to solve this is .. first create a StringBuffer Object(name it as buffer) by passing "input" as its constructor argument  and next you print "buffer.reverse()" by using sysout.. we will get the output meeting the question reuirements

Posted by ?????????? ?????    2015-01-12 10:14:07

Above Program is to Reverse a word given as input.

--String variable is object of class "String" in java.String is immutable i.e. once declared it's content can not be modified in that object variable.It is fixed size variable.

--So,For Modification "StringBuffer Class" is there in java which deals with the modification related with String.

--StringBuffer constructor is used in which contents are passed as that are given as input as follows:

--> StringBuffer sb=new StringBuffer(input);

--Reverse() is available with the StringBuffer objects which reverse the contents of that object in that object as follows.

--> System.out.println(sb.reverse());

Posted by Mânïshå Mùlchåndânï    2015-01-13 10:36:31

This dose is now closed and the winners are Mânïshå Mùlchåndânï,  for 'First Correct Comment', ?????????? ?????,  for 'Second Correct Comment'. The 'lucky liker' is Vikas Tiyagi. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-01-19 05:32:51

© meritcampus 2019

All Rights Reserved.

Open In App