Coding Challenge Day- 21: Problem 2: Reverse each word of String

Hello! So I’m doing a 30 day coding challenge where I solve a few questions every day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 21: Problem 2! Be sure to post your answers, queries etc in the comments!

Problem: Reverse each word of String

Sample input: Tech n Art
Output: hceT n trA

Solution:

package string;

import java.util.Stack;

public class Program012reverse_each_word_in_string {
	
	public static void reverse (String str){
		int length= str.length();
		
		Stack<Character> stack= new Stack<Character>();
		
		for(int i=0; i<length; i++){
			if(str.charAt(i)!=' '){
				stack.push(str.charAt(i));
			}
			
			else{
				while (!stack.isEmpty()){
				System.out.print(stack.pop());
				}
				
				System.out.print(" ");
			}
		}
		
		while (!stack.isEmpty()){
			System.out.print(stack.pop());
		}
		
		
	}
	
	public static void main (String [] args){
		String str= "Tech n Art";
		
		reverse(str);
	}

}

Download Code

Happy Learning!!

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: