Wednesday 17 June 2015

Program to reverse each word of a given string



//Program to reverse each word of a given string

import java.io.*;  //for IOException, BufferedReader, InputStreamReader
import java.util.*;  //for StringBuffer
public class WordReverse {
    public static void main(String[] args)throws IOException
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the string:");
    String str=br.readLine();
    StringTokenizer st=new StringTokenizer(str);
    String temp="";
    while(st.hasMoreTokens())
    {
        temp=temp+new StringBuffer(st.nextToken()).reverse().toString()+" ";
    }
        System.out.println("Reversed String is:");
        System.out.println(temp);
    }   
}



Output:

Enter the string:
Hello this is GsbProgramming
Reversed String is:
olleH siht si gnimmargorPbsG 

No comments:

Post a Comment