Reverse word of full sentence

后端 未结 16 1931
眼角桃花
眼角桃花 2020-12-06 20:04

I want to print string in reverse format:

Input: My name is Archit Patel

Output: Patel Archit is name My.

I\'ve tied the

16条回答
  •  我在风中等你
    2020-12-06 20:44

    You can use Stack for solving such queries:

    String input = "My name is Archit Patel";
    
            Stack st = new Stack(); 
    
            for(String word : input.split(" "))
                st.push(word); 
    
            Iterator it = st.iterator(); 
    
            while(it.hasNext()){
                System.out.print(st.pop()+" ");
            }
    
    Output String: "Patel Archit is name My"
    

提交回复
热议问题