A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Java program to reverse words of a string
Q:

Java program to reverse words of a string

0

Given a string and we have to reverse words using java program.

Example:

    Input:  
    I Love My Country

    Output:  
    Country My Love I

 

All Answers

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

Program

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

public class ReverseByWord 
{
	public static void main(String[] args) 
	{
		// create object of the string.
		String S;
		
		Scanner scan = new Scanner (System.in);
		
		// enter your string here.
		System.out.print("Enter the string : ");
		// will read string and store it in "S" for further process.
		S = scan.nextLine();
		
		StringTokenizer st = new StringTokenizer(S, " ");

		// strReverseLine is the function used to reverse a string.
		String strReversedLine = "";
		try
		{
			while(st.hasMoreTokens())
			{
				strReversedLine = st.nextToken() + " " + strReversedLine;
			}
			System.out.println("Reversed string by word is : " + strReversedLine);
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
	}
}

Output

Enter the string : I Love My Country
Reversed string by word is : Country My Love I 

 

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now