Inverse characters in a sentence

Im trying to cancel characters in a sentence without using the split function. I'm really close, but I miss the final letter. Can someone please point me in the right direction? Right now he is typing "This is a new sentence" as "sihT si wen cnetnes". I also included if (start == 0) , because the program will 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(); } 
+4
source share
6 answers

I think the error lies in the index, 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 substring(start,i) for the last i should be description.length instead of expression.length-1, so this will solve it.

Substring is open at the last index, so if you put substring(1, 10) , there will be a substring from 1 to 9. This may be a problem with the last word.

The thing with the first space is also a substring problem, let's say you read “this ...” the first time that it performs the substitution with start=0 and i = 4 so that you expect “this” but it really is “this”. The next read with start=4 and i=7 will be "is".

So, with changing the index, you can also remove if / else with start==0 .

+3
source
  • start means wordStart . Since I indicate a space, the next Start word should indicate after i.
  • Therefore, the last i should indicate after the last word char, should be length()
  • the if-then-else value is too large; in one case, you need to add a space: I'm pointing to a space.

It would be possible to loop unconditionally, and on i == length () break the middle of the loop code.

+4
source

Another variant

 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(); } 
+3
source
 String reverseString = "This is a new sentence"; System.out.println(new StringBuffer(reverseString).reverse().toString()); Syso prints : ecnetnes wen a si sihT 
+3
source

Put

 i <= sentence.length() 

In your for loop and change if to:

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

as

 substring(start,i) 

Returns the string to i, not included.

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

No split is visible.

+1
source

All Articles