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 6732793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:41:33+00:00 2026-05-26T10:41:33+00:00

which of the following is an efficient way to reverse words in a string

  • 0

which of the following is an efficient way to reverse words in a string ?

public String Reverse(StringTokenizer st){
    String[] words = new String[st.countTokens()];
    int i = 0;
    while(st.hasMoreTokens()){
       words[i] = st.nextToken();i++}

    for(int j = words.length-1;j--)
       output = words[j]+" ";}

OR

public String Reverse(StringTokenizer st, String output){        
    if(!st.hasMoreTokens()) return output;        
        output = st.nextToken()+" "+output;
        return Reverse(st, output);}       

public String ReverseMain(StringTokenizer st){       
    return Reverse(st, "");}

while the first way seems more readable and straight forward, there are two loops in it. In the 2nd method, I’ve tried doing it in tail-recursive way. But I am not sure whether java does optimize tail-recursive code.

  • 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-05-26T10:41:33+00:00Added an answer on May 26, 2026 at 10:41 am

    you could do this in just one loop

    public String Reverse(StringTokenizer st){
        int length = st.countTokens();
        String[] words = new String[length];
        int i = length - 1;
        while(i >= 0){
          words[i] = st.nextToken();i--}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've one interface which has following method signatures: public interface ITag { int M_Id
Which of the following ways is an efficient way of determining substring containment? if
Which is the more efficient way to create the following, hardcoded HTML or Javascript
I Have a object which the following field : boost::unordered_map<std::string, std::shared_ptr<Foo> > m_liste_; I
What are peoples' thoughts on the most performance efficient way to do the following
I am looking for an efficient formula working in Java which calculates the following
I am just looking for a efficient way for the following code since I
I'm looking for a simple and efficient way to solve the following problem: I
ello, I am looking for the most efficient way to do the following: Take
I have the following model public class Account { public int Id { get;

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.