Reverse string method

后端 未结 6 1617
暗喜
暗喜 2021-01-20 00:25

I am trying to solve the following problem but how do write the method that accepts String as an argument?

Write a method named printReverse

相关标签:
6条回答
  • 2021-01-20 00:42

    Something like this:

    public class StringUtils {
        public static String reverse(String forward) {
            String result = "";
            // Put your code here
            return result;
        }
    }
    
    0 讨论(0)
  • 2021-01-20 00:44

    I highly recommend you to go through a basic tutorial.

    You can simply do:

    private static String myReverse(String str) {
        String reverse = "";
        int length = str.length();
        for( int i = length - 1 ; i >= 0 ; i-- ) {
           reverse = reverse + str.charAt(i);
        }
        return reverse;
    }
    

    And in your main, you simply:

    String reversed = myReverse(in.nextLine());
    

    Note that the method is static because you're referring to it from a static manner (main method). If you don't want it to be static, you'll have to access it via an object.

    Also note that it's a good practice to always have curly brackets for for loops, even if it contains a single line.

    0 讨论(0)
  • 2021-01-20 00:50
    private static void printReverse (String org)
    {
    
      StringBuffer buffer = new StringBuffer(org);
      String reversedStr = buffer.reverse().toString();
      System.out.println("The reverse of the string \""
                + str + "\" is \"" + reversedStr + "\".");
    
    }
    

    in the main call the function

    printReverse(original);
    
    0 讨论(0)
  • 2021-01-20 00:55
    public class Dowhile {
      package dowhile;
      public static void main(String[] args) {
         // TODO code application logic here
         String message="i love java programming";
         int msglength = message.length();
    
         int index=msglength-1;
         while(index>=0)
         {
            System.out.print(message.charAt(index));
            index--;
         }  
      }
    }
    
    0 讨论(0)
  • 2021-01-20 00:59

    how do write the method that accepts String as an argument?

    public static String reverse(String forward) {
       char[] strChar = forward.toCharArray();
       String reverse = "";
    
       for( int i = strChar.length - 1 ; i >= 0 ; i-- ) 
           reverse = reverse + strChar[i];
    
       return reverse;
    }
    

    But for large string appending character with + operator can be inefficient. And reversing string with above approach will result in wrong for uni-code mismatches. As it reverse the code units but not character. There is actually a built-in support available to reverse a string using StringBuilder which works correctly:

    public static String reverse(String forward) {
       StringBuilder builder = new StringBuilder(forward);
       String reverse = builder.reverse().toString();
       return reverse;    
    }
    
    0 讨论(0)
  • 2021-01-20 01:01

    try this:

    private static String reverseString(String str)
    {
        String revString = "";
    
        for(int i=str.length()-1;i>=0;i--)
        {
            revString = revString + str.charAt(i);
        }
        return revString;
    }
    
    0 讨论(0)
提交回复
热议问题