Youtube Channel

Reverse a String (word) position in Java

package com.pack.string;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

public class StringPositionReverse {
    public static void main(String[] args) {
        int size;
        int i,j;
        ArrayList li=new ArrayList();
    String str="Hi i am indian";
    StringTokenizer stk=new StringTokenizer(str," ");
    size=stk.countTokens();
    li=new ArrayList();
   
    //adding tokens to arraylist
    for(i=0;i<size;i++)
    {
        li.add(stk.nextToken());
    }
       
    for(i=li.size()-1;i>=0;i--)
    {
        System.out.print(li.get(i)+" ");
    }
    }
}

Next PostNewer Post Previous PostOlder Post Home