Reverse a String (using StringBuilder class) in java.



import java.util.*;

 

public class Strings {

   public static void main(String args[]) {

     StringBuilder sb = new StringBuilder("HelloWorld");

    

     for(int i=0; i

       int front = i;

       int back = sb.length() - i - 1;

 

       char frontChar = sb.charAt(front);

       char backChar = sb.charAt(back);

 

       sb.setCharAt(front, backChar);

       sb.setCharAt(back, frontChar);

     }

 

     System.out.println(sb);

   }

}



Share to whatsapp

More Questions from Java Basic Codes Module 0

Write a function that takes in age as input and returns if that person is eligible to vote or not. A person of age > 18 is eligible to vote.


View

Write a function to calculate the factorial of a number.


View

Write an infinite loop using do while condition.


View

Find the maximum & minimum number in an array of integers.

[HINT : Read about Integer.MIN_VALUE & Integer.MAX_VALUE in Java]


View

Take an array of Strings input from the user & find the cumulative (combined) length of all those strings.


View

Print the spiral order matrix as output for a given matrix of numbers.


View

Write a function which takes in 2 numbers and returns the greater of those two.


View