Are You Feeling any difficulity while writing Core-Java Programs. Send those to me Here.

Friday, 22 November 2013

Get the input string from User using scanner object and reverse the string using for loop

Program: 
import java.util.Scanner;
public class HelloWorld{
     public static void main(String []args){
        System.out.println("Hello World");
        // Getting a string from user using Scanner

        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        // Printing the input string
        System.out.println(input);
        // Converting string to char array
        char ch[] = input.toCharArray();
        // Reversing the input string and saving output in other string
        char output[] = new char[ch.length];
        int j=0;
        for(int i=ch.length-1;i>=0;i--){
            output[j] = ch[i];
            j++;
        }
        // Printing the Reversed string
        System.out.println(String.valueOf(output));
     
     }
}

No comments:

Post a Comment