Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse characters in a sentence

Tags:

java

string

char

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();
}
like image 649
Calgar99 Avatar asked Dec 06 '22 09:12

Calgar99


2 Answers

  • start means wordStart. As i points to the space, the next wordStart should point after i.
  • Hence the last i should point after the last word char, should be length()
  • the if-then-else is too broad; a space has to be added in one case: i pointing at the space.

One could loop unconditionally, and on i == length() break in the middle of the loop code.

like image 86
Joop Eggen Avatar answered Dec 21 '22 15:12

Joop Eggen


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.

like image 30
Christian Vielma Avatar answered Dec 21 '22 16:12

Christian Vielma