2

I've created a small code that asks the user to input any word, then it will reverse it. I can't figure out how to reverse it so help is appreciated.

Here is my code:

package Test;
import java.util.Scanner;

public class TestMain {
public static void main(String[]args){

    String word = getWord();
    char[]wordArray;
    wordArray = word.toCharArray();
    System.out.print("NORMAL WORD: " + word + "\n");
    System.out.print("REVERSE WORD: ");


}

public static String getWord(){
    Scanner hold = new Scanner(System.in);
    String word;
    System.out.print("Enter a word:");
    word = hold.nextLine();
    return word;
}
}
2
  • 1
    Create a second array, loop over the first placing each character at the opposite end of the new array ... May something like this for example Commented Aug 27, 2015 at 3:26
  • Just a side note, but instead of putting "\n" at the end of your print statement you can just change it to a System.out.println() and it will automatically add the new line at the end. Commented Aug 27, 2015 at 3:44

6 Answers 6

2

StringBuilder have a reverse method.

 System.out.print("REVERSE WORD: " + new StringBuilder(word).reverse().toString());

Or if you don't want to use inbuilt methods

String result= "";
for(int i=word.length(); i>0; i--) {
    result+= word.charAt(i-1);
}  
 System.out.print("REVERSE WORD: " + result);
Sign up to request clarification or add additional context in comments.

Comments

1
public class test {
    public static void main(String[] args) {
        String word = "Stack Overflow";
        char[] wordArray = word.toCharArray();
        System.out.println("NORMAL WORD=" + Arrays.toString(wordArray));
        test.reverse(wordArray);
        System.out.println("REVERSE WORD=" + Arrays.toString(wordArray));
    }

    public static void reverse(char[] array) {
        if (array == null) {
            return;
        }
        int i = 0;
        int j = array.length - 1;
        char tmp;
        while (j > i) {
            tmp = array[j];
            array[j] = array[i];
            array[i] = tmp;
            j--;
            i++;
        }
    }
}

Output:

NORMAL WORD=[S, t, a, c, k,  , O, v, e, r, f, l, o, w]
REVERSE WORD=[w, o, l, f, r, e, v, O,  , k, c, a, t, S]

Comments

1

Do this:
1. Convert the string into a character array.
2. Swap the elements (first and last -> second and second last -> ... Till the middle).
3. You will get a linear complexity of O(n/2) = O(n).

public static String reverseWord(String str)
{
    if(str.length()>0)
    {
        char arr[] = str.toCharArray();
        int length = arr.length;
        for(int i=0; i<length/2; i++)
        {
            char temp = arr[i];
            arr[i] = arr[length-i-1];
            arr[length-i-1] = temp;
        }
        return new String(arr);
    }
    return str;
}

Comments

0

I use StringBuilder for constructed the reversed string. In your main method.

    String word = getWord();
    StringBuilder sb = new StringBuilder();
    for(char c : word.toCharArray()) {
        sb.insert(0, c);
    }
    String reversed = sb.toString();
    System.out.print("NORMAL WORD: " + word);
    System.out.print("REVERSE WORD: " + reversed);

Comments

0

Let's look at the code in main:

char[]wordArray = word.toCharArray();

The String is a group of chars with one after the other.

We need to add each char to the beginning of the result in a loop:

String result = "";
for(char wordChar : word.toCharArray()) {
    result = wordChar + result;
}

This would cause the first char to go last and the second to go second to last etc. This way we can reverse the order of the String.

Comments

0
public static void main(String args[]) {
    String a = "12345";
    char [] original= a.toCharArray();
    char[] reverse = new char[a.length()];
    int j =0;
    for(int i=reverse.length-1; i>=0; i--) {
        reverse[j] = original[i]; 
        j++;
    }

    System.out.println(new String(reverse));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.