Calgar99
Calgar99

Reputation: 1688

Reverse characters in a sentence

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();
}

Upvotes: 4

Views: 652

Answers (6)

Arundev
Arundev

Reputation: 1934

String reverseString = "This is a new sentence";
System.out.println(new StringBuffer(reverseString).reverse().toString());

Syso prints : ecnetnes wen a si sihT

Upvotes: 3

Christian Vielma
Christian Vielma

Reputation: 15995

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.

Upvotes: 3

Whippet
Whippet

Reputation: 322

import java.util.Stack;

public class Class {
    public static void main(String[] args) {
        String input = "This is a sentence";
        char[] charinput = input.toCharArray();
        Stack<String> stack = new Stack<String>();
        for (int i = input.length() - 1; i >= 0; i--) {
            stack.push(String.valueOf(charinput[i]));
        }
        StringBuilder StackPush = new StringBuilder();
        for (int i = 0; i < stack.size(); i++) {
            StackPush.append(stack.get(i));
        }
        System.out.println(StackPush.toString());
    }
}

Not a split to be seen.

Upvotes: 1

BobTheBuilder
BobTheBuilder

Reputation: 19284

Put

i <= sentence.length()

In your for loop and change the if to:

if(i == sentence.length() || sentence.charAt(i)== ' ')

as

substring(start,i)

Returns the string up to i, not included.

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109547

  • 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.

Upvotes: 4

maxivis
maxivis

Reputation: 1787

Another option

private String reverse (String originalString) {
    StringBuilder reverseString = new StringBuilder();
    for (int i = originalString.length() - 1; i >= 0; i--) {
        reverseString.append(originalString.charAt(i));
    }
    return reverseString.toString();
}

Upvotes: 3

Related Questions