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

Reverse a String (using StringBuilder class) in java.


View

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


View

For a given matrix of N x M, print its transpose in java.


View

Write a function to calculate the product of 2 numbers.


View

Write a function that calculates the Greatest Common Divisor of 2 numbers.


View

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


View

Write a program to print Fibonacci series of n terms where n is input by user :

0 1 1 2 3 5 8 13 21 ..... 

In the Fibonacci series, a number is the sum of the previous 2 numbers that came before it.


View