reverse string function in java code example

Example 1: reverse string java

// Use StringBuilder for non-thread environment, it is faster
String string="whatever";
String reverse = new StringBuilder(string).reverse().toString();
System.out.println(reverse);

Example 2: how to reverse a string in java

public class ReverseString {
    public static void main(String[] args) {
        String s1 = "neelendra";
        for(int i=s1.length()-1;i>=0;i--)
            {
                System.out.print(s1.charAt(i));
            }
    }
}

Example 3: java string reverse

String rev = new StringBuilder("Your String").reverse().toString();

Example 4: reverse a string in java using input

public class Main {

    public static void main(String[] args) {
	// Write a Java program to reverse a string.
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the String:");
        char[] letters = input.nextLine().toCharArray();
        System.out.println(" Reverse String");
        for(int i = letters.length -1; i >= 0; i--){
            System.out.print(letters[i]);
        }
    }
}

Example 5: reverse string array java

import java.util.Arrays;
public class ReverseStringArrayInJava
{
   public static void main(String[] args)
   {
      String[] strHierarchy = new String[]{"Junior Developer","Senior Developer","Team Lead","Project Manager","Senior Manager","CEO"};
      System.out.println("Given string array: " + Arrays.toString(strHierarchy));
      for(int a = 0; a < strHierarchy.length / 2; a++)
      {
         String strTemp = strHierarchy[a];
         strHierarchy[a] = strHierarchy[strHierarchy.length - a - 1];
         strHierarchy[strHierarchy.length - a - 1] = strTemp;
      }
      System.out.println("Reversed string array: ");
      for(int a = 0; a < strHierarchy.length; a++)
      {
         System.out.println(strHierarchy[a]);
      }
   }
}

Tags:

Cpp Example