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

Take an array of numbers as input and check if it is an array sorted in ascending order.

Eg : { 1, 2, 4, 7 } is sorted in ascending order.

       {3, 4, 6, 2} is not sorted in ascending order.


View

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


View

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

Take an array of names as input from the user and print them on the screen.


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

Write an infinite loop using do while condition.


View

Enter 3 numbers from the user & make a function to print their average.


View