Java program to print reversed string by word in given line


Program

package com.candidjava;

import java.util.Scanner;
import java.util.StringTokenizer;

public class ReverseStringWord
{

	public static void main(String[] args)
	{
		System.out.println("Enter the String :");
		Scanner sc = new Scanner(System.in);

		String s = sc.nextLine();

		// specify delimiter as " " space
		StringTokenizer st = new StringTokenizer(s, " ");

		String a = "";

		while (st.hasMoreTokens())
		{

			a = st.nextToken() + " " + a;

		}

		System.out.println("Reversed string by word is : " + a);
	}
}

Output

Enter the String :

Welcome to Candid Java Program

Reversed string by word is : Program Java Candid to Welcome 




Related Post

Comments


©candidjava.com