Reverse a string in Java

前端 未结 30 2461
礼貌的吻别
礼貌的吻别 2020-11-21 13:12

I have \"Hello World\" kept in a String variable named hi.

I need to print it, but reversed.

How can I do this? I understand there

相关标签:
30条回答
  • 2020-11-21 13:58

    Procedure :

    We can use split() to split the string .Then use reverse loop and add the characters.


    Code snippet:

    class test
    {
      public static void main(String args[]) 
      {
          String str = "world";
          String[] split= str.split("");
    
          String revers = "";
          for (int i = split.length-1; i>=0; i--)
          {
            revers += split[i];
          }
          System.out.printf("%s", revers);
       }  
    }
    
     //output : dlrow
    

    0 讨论(0)
  • 2020-11-21 13:58

    recursion:

     public String stringReverse(String string) {
        if (string == null || string.length() == 0) {
            return string;
        }
        return stringReverse(string.substring(1)) + string.charAt(0);
     }
    
    0 讨论(0)
  • 2020-11-21 13:58
        //Solution #1 -- Using array and charAt()
        String name = "reverse"; //String to reverse
        Character[] nameChar =  new Character[name.length()]; // Declaring a character array with length as length of the String which you want to reverse.
        for(int i=0;i<name.length();i++)// this will loop you through the String
        nameChar[i]=name.charAt(name.length()-1-i);// Using built in charAt() we can fetch the character at a given index. 
        for(char nam:nameChar)// Just to print the above nameChar character Array using an enhanced for loop
        System.out.print(nam);
    
    
        //Solution #2 - Using StringBuffer and reverse ().
        StringBuffer reverseString = new StringBuffer("reverse");
        System.out.println(reverseString.reverse()); //reverse () Causes the character sequence to be replaced by the reverse of the sequence.
    
    0 讨论(0)
  • 2020-11-21 13:59

    1. Using Character Array:

    public String reverseString(String inputString) {
        char[] inputStringArray = inputString.toCharArray();
        String reverseString = "";
        for (int i = inputStringArray.length - 1; i >= 0; i--) {
            reverseString += inputStringArray[i];
        }
        return reverseString;
    }
    

    2. Using StringBuilder:

    public String reverseString(String inputString) {
        StringBuilder stringBuilder = new StringBuilder(inputString);
        stringBuilder = stringBuilder.reverse();
        return stringBuilder.toString();
    }
    

    OR

    return new StringBuilder(inputString).reverse().toString();
    
    0 讨论(0)
  • 2020-11-21 13:59
    System.out.print("Please enter your name: ");
    String name = keyboard.nextLine();
    
    String reverse = new StringBuffer(name).reverse().toString();
    String rev = reverse.toLowerCase();
    System.out.println(rev);
    

    I used this method to turn names backwards and into lower case.

    0 讨论(0)
  • 2020-11-21 14:00

    public String reverseWords(String s) {

        String reversedWords = "";
    
        if(s.length()<=0) {
            return reversedWords;
        }else if(s.length() == 1){
            if(s == " "){
                return "";
            }
            return s;
        }
    
        char arr[] = s.toCharArray();
        int j = arr.length-1;
        while(j >= 0 ){
            if( arr[j] == ' '){
                reversedWords+=arr[j];
            }else{
                String temp="";
                while(j>=0 && arr[j] != ' '){
                    temp+=arr[j];
                    j--;
                }
                j++;
                temp = reverseWord(temp);
                reversedWords+=temp;
            }
            j--;
    
        }
    
        String[] chk = reversedWords.split(" ");
    
        if(chk == null || chk.length == 0){
            return "";
        }
    
        return reversedWords;
    
    
    
    }
    
    public String reverseWord(String s){
    
        char[] arr = s.toCharArray();
    
        for(int i=0,j=arr.length-1;i<=j;i++,j--){
            char tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
        return String.valueOf(arr);
    
    }
    
    0 讨论(0)
提交回复
热议问题