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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:54:06+00:00 2026-06-18T20:54:06+00:00

Im trying to reverse characters in a sentence without using the split function. Im

  • 0

Im trying to reverse characters in a sentence without using the split function. Im really close but I am missing the final letter. Can some one please point me in the right direction? Right now it prints “This is a new sentence” as “sihT si a wen cnetnes” Also I included if(start == 0) because the program would skip the initial space character, but I don’t understand why?

static String reverseLetters(String sentence)
    StringBuilder reversed = new StringBuilder("");
        int counter = 0;
        int start = 0;
        String word;

        for(int i = 0; i <= sentence.length()-1 ; i++ )
        {
            if(sentence.charAt(i)== ' '|| i == sentence.length()-1 )
            {
                StringBuilder sb = new StringBuilder("");
                sb.append(sentence.substring(start,i));
                if(start == 0)
                {
                start = i;
                word = sb.toString();
                reversed.append(reverseChar(word));
                reversed.append(' ');
                }
                else
                {
                    start = i;
                    word = sb.toString();
                    reversed.append(reverseChar(word));
                }
            }


  return reversed.toString();
        }

static String reverseChar (String word)
{
    StringBuilder b = new StringBuilder("");
    for(int idx = word.length()-1; idx >= 0; idx -- )
    {
        b.append(word.charAt(idx));
    }
    return b.toString();
}
  • 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-18T20:54:07+00:00Added an answer on June 18, 2026 at 8:54 pm

    I think the error lies in the index, the for should be

     for(int i = 0; i <= sentence.length() ; i++ )
    

    Then if should be:

    if (sentence.charAt(i==0?0:i-1)== ' '|| i == sentence.length() )

    For me the error will be that the substring(start,i) for the last one i should be sentence.length instead of sentence.length-1, so this would solve it.

    Substring is open in the last index, so if you put substring(1, 10) will be substring from 1 to 9. That might be the problem with last word.

    The thing with the first space is also the problem with substring, let’s say you’re reading “this is…” the first time it will do a subtring with start=0 and i = 4 so you expect “this ” but it really is “this”. The next reading, with start=4 and i=7 will be ” is”.

    So with the change of the index you should be able to remove the if/else with start==0 too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to reverse a string in Lua without using the string.reverse() function.
I'm trying to reverse Enumerable (like Array) without using reverse method, but using reverse_each
I'm trying to read the characters from a file in reverse order using lseek.
I am attempting to reverse a string without using the reverse function, classes and
Trying to reverse a string but getting error in Aggregate function private string Reverse(string
I'm currently trying to reverse geocode a series of lat/long co-ordinates using the Virtual
I was trying to reverse a number in PL/SQL. It's working fine, but when
I am trying to reverse engineer a c code, but this part of assembly
I am really going beserk here. I am trying to reverse the words in
I'm trying to reverse engineer a function over which i have no source, and

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.