Tolga Ozses
0
Q:

java string reverse

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));
            }
    }
}
13
// reverse a string using character array
class ReverseUsingCharacterArray
{
   public static void main(String[] args)
   {
      String str = "HelloWorldJava";
      char[] ch = str.toCharArray();
      for(int a = ch.length - 1; a >= 0; a--)
         System.out.print(ch[a]);
   }
}
1
// reverse a string using toCharArray() method
class ReverseUsingToCharArrayMethod
{
   public static void main(String[] args)
   {
      String str = "Hello World Java";
      char[] chTemp = str.toCharArray();
      int left, right = 0;
      right = chTemp.length - 1;
      for(left = 0; left < right ; left++, right--)
      {
         // swap values
         char temp = chTemp[left];
         chTemp[left] = chTemp[right];
         chTemp[right]=temp;
      }
      for(char c : chTemp)
         System.out.print(c);
      System.out.println();
   }
}
1
// reverse a string using reverse() method of StringBuilder class
class ReverseUsingReverseMethod
{
   public static void main(String[] args)
   {
      String str = "Hello world Java";
      StringBuilder sb = new StringBuilder();
      // append string to StringBuilder
      sb.append(str);
      sb = sb.reverse();
      // printing reversed String
      System.out.println(sb);
   }
}
1
// reverse a string using ByteArray
class ReverseStringByteArray
{
   public static void main(String[] args)
   {
      String input = "HelloWorld";
      // getBytes() method to convert string into bytes[].
      byte[] strByteArray = input.getBytes();
      byte[] output = new byte[strByteArray.length];
      // store output in reverse order
      for(int a = 0; a < strByteArray.length; a++)
         output[a] = strByteArray[strByteArray.length - a - 1];
      System.out.println(new String(output));
   }
}
1
String str = "Hello";
String reverse(String str){
  StringBuilder sb = new StringBuilder();
  sb.append(str);
  sb.reverse();
  return sb.toString();
}
9
As we know that String is immutable. String class do not have reverse() method.
2
//java program to reverse array using for loop
public class ReverseArrayDemo 
{
   public static void main(String[] args) 
   {
      int[] arrNumbers = new int[]{2, 4, 6, 8, 10};  
      System.out.println("Given array: ");  
      for(int a = 0; a < arrNumbers.length; a++)
      {
         System.out.print(arrNumbers[a] + " ");
      }
      System.out.println("Reverse array: ");
      // looping array in reverse order
      for(int a = arrNumbers.length - 1; a >= 0; a--) 
      {  
         System.out.print(arrNumbers[a] + " ");  
      }
   }
}
1
Solution 1 

public static String StrReverse(String str) {

String reverse="";

for(int i=str.length()-1; i >= 0; i--)

reverse += str.toCharArray()[i];

 

return  reverse;

}

 

Solution 2 (using string buffer)

public  static String  Reverse(String str) {

return new StringBuffer(str).reverse().toString());

}
1
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]);
      }
   }
}
1

New to Communities?

Join the community