Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8243421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:30:50+00:00 2026-06-07T21:30:50+00:00

This code is inside the main function: Scanner input = new Scanner(System.in); System.out.println(Type a

  • 0

This code is inside the main function:

Scanner input = new Scanner(System.in);

System.out.println("Type a sentence");
String sentence = input.next();

Stack<Character> stk = new Stack<Character>();
int i = 0;

while (i < sentence.length())
{
    while (sentence.charAt(i) != ' ' && i < sentence.length() - 1)
    {
        stk.push(sentence.charAt(i));
        i++;
    }
    stk.empty();
    i++;
}

And this is the empty() function:

public void empty()
{
    while (this.first != null)
        System.out.print(this.pop());
}

It doesn’t work properly, as by typing example sentence I am getting this output: lpmaxe. The first letter is missing and the loop stops instead of counting past the space to the next part of the sentence.

I am trying to achieve this:

This is a sentence —> sihT si a ecnetnes

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:30:52+00:00Added an answer on June 7, 2026 at 9:30 pm

    Per modifications to the original post, where the OP is now indicating that his goal is to reverse the letter order of the words within a sentence, but to leave the words in their initial positions.

    The simplest way to do this, I think, is to make use of the String split function, iterate through the words, and reverse their orders.

    String[] words = sentence.split(" "); // splits on the space between words
    
    for (int i = 0; i < words.length; i++) {
        String word = words[i];
        System.out.print(reverseWord(word));
    
        if (i < words.length-1) {
            System.out.print(" "); // space after all words but the last
        }
    }
    

    Where the method reverseWord is defined as:

    public String reverseWord(String word) {
        for( int i = 0; i < word.length(); i++) {
            stk.push(word.charAt(i));
        }
        return stk.empty();
    }
    

    And where the empty method has been changed to:

    public String empty() {
        String stackWord = "";
        while (this.first != null)
            stackWord += this.pop();
        return stackWord;
    }
    

    Original response

    The original question indicated that the OP wanted to completely reverse the sentence.

    You’ve got a double-looping construct where you don’t really need it.

    Consider this logic:

    1. Read each character from the input string and push that character to the stack
    2. When the input string is empty, pop each character from the stack and print it to screen.

    So:

    for( int i = 0; i < sentence.length(); i++) {
        stk.push(sentence.charAt(i));
    }
    stk.empty();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this code inside WPF application: public static string EncryptString(string stringToEncrypt) { SHA256
I have this code snippet inside a function that checks if an object exists
I have this code: function render(str) { var main = document.createElement('div'); with(main.style) { //style
So I have this code inside my lib/ folder: class GlobalConfig::SetHelper def self.yes_no_input(configuration) value
I am using this code (inside an AsyncTask) to download files: URL u =
I have this code inside thread and when I call terminate it terminates fast
I have a jsp page which needs a java code. This java code inside
This is my code inside smarty tpl file(or for any simple HTML): <img src=../images/blah.jpg
Will this code ever wait on the mutex inside the producer's void push(data) ?
I have this code that shows all the images inside 'images' directory but it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.