Pages

String Token Reverse


A java program to tokenize a string using single space as delimiter and reverse the words and then append these reverse words.

Method you should implement

1. Tokenize the string using ' ' (single space) as the delimiter. This method should return the array of tokens/words.

public String[] getTokens(String data)

2. Pass the array of tokens/words obtained in the previous method to the method below to reverse the words. Once the words are reversed, append the words

public String reverseAndAppend(String []data)

Example:
Input: Hello World
Output: olleH dlroW

package com.psl;

public class Client {

    /**
     * @param args
     */
   
    public String[] getTokens(String data) {
        String arr[] = data.split(" ");
        return arr;
    }
   
    public String reverseAndAppend(String [] data) {
        String str = "";
        for(String s : data) {
            StringBuffer string = new StringBuffer(s);
            str = str + string.reverse() + " ";
        }
        return str.trim();
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        String myString = "Hello Welcome to j2sehumt.blogspot.com";
        Client demo = new Client();
        System.out.println(demo.reverseAndAppend(demo.getTokens(myString)));

    }

}

No comments:

Post a Comment