Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

reverse string in java

String reverse = new StringBuilder(string).reverse().toString();
Comment

reverse a string in java

public class StringReverseExample{
   public static void main(String[] args) {
      String string = "abcdef";
      String reverse = new StringBuffer(string).reverse().toString();
      System.out.println("
String before reverse: "+string);
      System.out.println("String after reverse: "+reverse);
   }
}
Comment

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));
            }
    }
}
Comment

string reverse in java

String str = "Hello";
String reverse(String str){
  StringBuilder sb = new StringBuilder();
  sb.append(str);
  sb.reverse();
  return sb.toString();
}
Comment

java Write a program to reverse an array or string

// Iterative java program to reverse an
// array
public class GFG {
     
   /* Function to reverse arr[] from
    start to end*/
    static void rvereseArray(int arr[],
                    int start, int end)
    {
        int temp;
          
        while (start < end)
        {
            temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
    }    
      
    /* Utility that prints out an
    array on a line */
    static void printArray(int arr[],
                            int size)
    {
        for (int i = 0; i < size; i++)
             System.out.print(arr[i] + " ");
          
         System.out.println();
    }
 
    // Driver code
    public static void main(String args[]) {
         
        int arr[] = {1, 2, 3, 4, 5, 6};
        printArray(arr, 6);
        rvereseArray(arr, 0, 5);
        System.out.print("Reversed array is 
");
        printArray(arr, 6);
        
    }
}
 
Comment

how to reverse a string in java without function

String reverse(String s) { 
   if(s.length() == 0) 
     return ""; 
   return s.charAt(s.length() - 1) + reverse(s.substring(0,s.length()-1)); 
 }
Comment

java reverse string

// Not the best way i know but i wanted to challenge myself to do it this way so :)
public static String reverse(String str) {
  char[] oldCharArray = str.toCharArray();
  char[] newCharArray= new char[oldCharArray.length];
  int currentChar = oldCharArray.length-1;
  for (char c : oldCharArray) {
    newCharArray[currentChar] = c;
    currentChar--;
  }
  return new String(newCharArray);
}
Comment

Reverse a String in Java

public class ReverseStringByFavTutor

{

   public static void main(String[] args) {


       String stringExample  =  "FavTutor";

       System.out.println("Original string: "+stringExample);


       // Declaring a StringBuilder and converting string to StringBuilder

       StringBuilder reverseString = new StringBuilder(stringExample);


       reverseString.reverse();  // Reversing the StringBuilder


       // Converting StringBuilder to String

       String result = reverseString.toString();


       System.out.println("Reversed string: "+result); // Printing the reversed String


   }

}
Comment

java String revers

 public static void main(String[] args)
    {
        String input = "Geeks for Geeks";
 
        StringBuilder input1 = new StringBuilder();
 
        // append a string into StringBuilder input1
        input1.append(input);
 
        // reverse StringBuilder input1
        input1.reverse();
 
        // print reversed String
        System.out.println(input1);
    }
Comment

String reverse in java

1)
String str = "Hello";
String result = "";

  for(int i = str.length()-1; i>=0; i--){
     result += str.charAt(i); // first solution, charAt method
 //  result += str1.substring(i, i+1); // first solution,  substring method
   }
   System.out.println(result);
}

Comment

String Reverse Program in Java.

// java program to reverse a word
  
import java.io.*;
import java.util.Scanner;
  
class GFG {
    public static void main (String[] args) {
        
        String str= "Geeks", nstr="";
        char ch;
        
      System.out.print("Original word: ");
      System.out.println("Geeks"); //Example word
        
      for (int i=0; i<str.length(); i++)
      {
        ch= str.charAt(i); //extracts each character
        nstr= ch+nstr; //adds each character in front of the existing string
      }
      System.out.println("Reversed word: "+ nstr);
    }
}
  
//Contributed by Tiyasa
Comment

String reverse in Java

StringBuilder sb1 = new StringBuilder("Mahmut");
        sb1.reverse();
        System.out.println(sb1); // tumhaM
Comment

reverse words in a string java

// Java Program to reverse a String
// without using inbuilt String function
import java.util.regex.Pattern;
public class Exp {
 
    // Method to reverse words of a String
    static String reverseWords(String str)
    {
 
        // Specifying the pattern to be searched
        Pattern pattern = Pattern.compile("s");
 
        // splitting String str with a pattern
        // (i.e )splitting the string whenever their
        // is whitespace and store in temp array.
        String[] temp = pattern.split(str);
        String result = "";
 
        // Iterate over the temp array and store
        // the string in reverse order.
        for (int i = 0; i < temp.length; i++) {
            if (i == temp.length - 1)
                result = temp[i] + result;
            else
                result = " " + temp[i] + result;
        }
        return result;
    }
 
    // Driver methods to test above method
    public static void main(String[] args)
    {
        String s1 = "Welcome to geeksforgeeks";
        System.out.println(reverseWords(s1));
 
        String s2 = "I love Java Programming";
        System.out.println(reverseWords(s2));
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java loop backwards through array 
Java :: list java initialize 
Java :: linked list sorting in java 
Java :: how to test how many of one character is in a string java 
Java :: java coding problems 
Java :: leetcode patterns 
Java :: String array into LinkedList java 
Java :: list in java 
Java :: sum of all numbers in array java 
Java :: get value textfield java 
Java :: how to iterate hashset in java 8 
Java :: find shortest string in array java 
Java :: java random seed 
Java :: minecraft java 
Java :: Java Copying Arrays Using copyOfRange() method 
Java :: byte data type in java example 
Java :: ++i vs i++ java 
Java :: java array to list 
Java :: float random class java 
Java :: Display double in decimal places java 
Java :: calendar java add 1 day 
Java :: zip file java 
Java :: how to round up in java 
Java :: Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema 
Java :: java remove duplicates from list by property 
Java :: how to divide two ints and get a double java 
Java :: java calendar hour vs hour of day 
Java :: java generate random id 
Java :: java random usage 
Java :: java change text on click 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =