Reverse a word

Reverse a word

Write a method that will take a text and reverse it.

Example 1:


input data:
slay
output:
yals

Example 2:


input data:
brain
output:
niarb
View Code Template
View Java Solution

Seda
Seda Kunda is a web designer and developer with a degree in Computer Science and a great passion for code. Besides code, she enjoys pepperoni pizza, watching the bachelor and sleeping in on Saturdays.
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

2 thoughts on “Reverse a word

  1. public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print(“Please enter a word to reverse: “);
    String word = s.nextLine();
    reverse(word);
    }
    static void reverse(String word) {
    String x = null ;
    int length = word.length();

    for ( int i = length – 1 ; i >= 0 ; i– )
    x = x + word.charAt(i);

    System.out.println(“Reverse of entered string is: “+x);
    }
    }

Comments are closed.