MrCool
MrCool

Reputation: 43

How to reverse character elements in array using java?

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

Upvotes: 2

Views: 8205

Answers (6)

anirban.at.web
anirban.at.web

Reputation: 359

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

Upvotes: 1

2787184
2787184

Reputation: 3881

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]

Upvotes: 1

whitecoffee
whitecoffee

Reputation: 76

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

Upvotes: 0

Moishe Lipsker
Moishe Lipsker

Reputation: 3032

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.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122008

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

Upvotes: 2

Ferdinand Neman
Ferdinand Neman

Reputation: 700

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

Upvotes: 0

Related Questions