• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » WAP to Reverse Words without Changing Order & Split

WAP to Reverse Words without Changing Order & Split

Last Updated on: July 26, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Reverse Words without Changing Order & Split

Reverse Words without Changing Order & Split: This Java program takes an input string and reverses the order of characters within each word while keeping the words themselves in the same order. The program uses a stack data structure to achieve this.

Reverse Words without Changing Order & Split

package com.softwaretestingo.interviewprograms;
import java.util.Stack;
public class InterviewPrograms27 
{
	/*
	 * Input: reverse me without split 
	 * Output: esrever em tuohtiw tilps
	 */
	public static void main(String[] args) 
	{
		String str = "reverse me without split";
		Stack st=new Stack<Character>();
		for (int i = 0; i < str.length(); ++i) 
		{
			if (str.charAt(i) != ' ')
				st.push(str.charAt(i));
			else {
				while (st.empty() == false) 
				{
					System.out.print(st.pop());
				}
				System.out.print(" ");
			}
		}
		while (st.empty() == false) 
		{
			System.out.print(st.pop());
		}
	}
}

Output

esrever em tuohtiw tilps

Here’s a breakdown of the program:

  1. Import the required Java classes:
    • The program imports the Stack class from the java.util package, which is used to implement the stack data structure.
  2. Define the main method:
    • The entry point of the program is the main method.
  3. Initialize variables:
    • The input string str is initialized with the value “reverse me without split”.
    • A Stack object named st is created to store characters temporarily.
  4. Loop through the input string:
    • The program iterates through each character of the input string using a for loop.
  5. Stack usage:
    • If the current character is not a space, it means it’s a part of a word, so it is pushed onto the stack.
    • If a space is encountered, it means a word has ended, and the program empties the stack by popping and printing each character in reverse order.
    • After printing the reversed word, a space is printed to separate the words.
  6. Print the reversed string:
    • After the loop, there might still be characters left in the stack. The program empties the stack again and prints the remaining characters.

Overall, this program reverses the characters within each word in the input string without using the split method, which splits the string into words based on spaces. Instead, it utilizes a stack to reverse the characters within each word individually while maintaining their original order. The output will be “esrever em tuohtiw tilps”, which is each word of the input string reversed.

    Simple Java Programs for Beginners
    Simple Java Programs for Beginners Example
    Java Array Programs
    Java Array Programs
    WAP to Sort Array Of String According To String Length
    WAP to Sort Array Of String According To String Length
    WAP to Split An Array Into Two Equal Sum Subarrays
    WAP to Split An Array Into Two Equal Sum Subarrays
    Order Character Based On Number Of Occurrences
    WAP to Order Character Based On Number Of Occurrences
    WAP to Reverse Order Of Words By Preserving Space Position
    WAP to Reverse Order Of String By Preserving Space Position?
    WAP to Replace a Specific Character with # Increasing Number
    WAP to Replace a Specific Character with # Increasing Number
    WAP to convert 01230 To 03210
    WAP to Convert 01230 To 03210
    WAP to Cyclically Shifting the Input String
    WAP to Cyclically Shifting the Input String
    WAP to Print Unique Digits Of an Array
    WAP to Print Unique Digits Of an Integer Array

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers