Reverse string printing method
Asked Answered
T

7

4

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 that accepts a String as an argument and prints the characters in the opposite order. If the empty string is passed as an argument, the method should produce no output. Be sure to write a main method that convincingly demonstrates your program in action. Do not use the reverse method of the StringBuilder or StringBuffer class!

So far I have solved it in a easier manner:

import java.util.Scanner;

class ReverseString {
    public static void main(String args[]) {
        String original, reverse = "";
        Scanner in = new Scanner(System.in);

        System.out.println("Enter a string to reverse");
        original = in.nextLine();

        int length = original.length();

        for (int i = length - 1; i >= 0; i--)
            reverse = reverse + original.charAt(i);

        System.out.println("Reverse of entered string is: " + reverse);
    }
}
Teyde answered 1/12, 2013 at 12:43 Comment(0)
W
4

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.

Wire answered 1/12, 2013 at 12:46 Comment(0)
O
3

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;    
}
Or answered 1/12, 2013 at 12:54 Comment(0)
B
2

Something like this:

public class StringUtils {
    public static String reverse(String forward) {
        String result = "";
        // Put your code here
        return result;
    }
}
Baudoin answered 1/12, 2013 at 12:46 Comment(0)
H
2

Using Java 9 you can implement something like this. This code works with both regular characters and surrogate pairs:

public static void printReverse(String str) {
    // character code points
    str.codePoints()
            // character as string
            .mapToObj(Character::toString)
            // concatenate in reverse order
            .reduce((a, b) -> b + a)
            // output
            .ifPresent(System.out::println);
}

public static void main(String[] args) {
    // regular characters
    printReverse("lorem ipsum");
    // surrogate pairs
    printReverse("\uD835\uDD43\uD835\uDD46R\uD835\uDD3C\uD835\uDD44" +
            " \uD835\uDD40P\uD835\uDD4A\uD835\uDD4C\uD835\uDD44");
}

Output:

muspi merol
π•„π•Œπ•ŠP𝕀 𝕄𝔼R𝕆𝕃

See also: Is there any other way to remove all whitespaces in a string?

Hypsometer answered 13/4, 2021 at 1:58 Comment(0)
T
-1
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);
Throw answered 1/12, 2013 at 12:48 Comment(0)
S
-1

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;
}
Stuckup answered 29/4, 2014 at 10:29 Comment(0)
V
-1
package dowhile;
public class 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--;
        }
    }
}

Output:

gnimmargorp avaj evol i
Vallie answered 14/11, 2014 at 8:9 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.