Q:

Write a Java program to reverse a word

0

Write a Java program to reverse a word
Sample Output:

Input a word: dsaf
Reverse word: fasd

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.*;
 public class Exercise61 {
     public static void main(String[] args){	
     Scanner in = new Scanner(System.in);
     System.out.print("\nInput a word: ");
	 String word = in.nextLine();
	 word = word.trim();
	 String result = ""; 
     char[] ch=word.toCharArray();  
	 for (int i = ch.length - 1; i >= 0; i--) {
			 result += ch[i];
		 }
	 System.out.println("Reverse word: "+result.trim()); 
	 }			
}

Sample Output:

Input a word: dsaf                                                     
Reverse word: fasd

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now