Hello I want to do reverse each word of the String in Java example if input String is "Hello World" then the output should be "olleH dlroW".
You need to do this on each word after you split
into an array
of words.
public reverse(String word) {
char[] chs = word.toCharArray();
int i=0, j=chs.length-1;
while (i < j) {
// swap chs[i] and chs[j]
char t = chs[i];
chs[i] = chs[j];
chs[j] = t;
i++; j--;
}
}
Heres a method that takes a string and reverses it.
public String reverse ( String s ) {
int length = s.length(), last = length - 1;
char[] chars = s.toCharArray();
for ( int i = 0; i < length/2; i++ ) {
char c = chars[i];
chars[i] = chars[last - i];
chars[last - i] = c;
}
return new String(chars);
}
First you need to split the string into words like this
String sample = "hello world";
String[] words = sample.split(" ");
use java StringBuffer.reverse()
String[] toks = new String("Hellow World");
for (String s : toks)
{
StringBuffer sb = new StringBuffer(s);
sb.reverse();
System.out.print(sb);
}
This should do the trick. This will iterate through each word in the source string, reverse it using StringBuffer
's built-in reverse()
method, and output the reversed word.
String source = "Hello World";
for (String part : source.split(" ")) {
System.out.print(new StringBuffer(part).reverse().toString());
System.out.print(" ");
}
Output:
olleH dlroW
Notes: Commenters have correctly pointed out a few things that I thought I should mention here. This example will append an extra space to the end of the result. It also assumes your words are separated by a single space each and your sentence contains no punctuation.
public static void main(String[] args) {
System.out.println(eatWord(new StringBuilder("Hello World This Is Tony's Code"), new StringBuilder(), new StringBuilder()));
}
static StringBuilder eatWord(StringBuilder feed, StringBuilder swallowed, StringBuilder digested) {
for (int i = 0, size = feed.length(); i <= size; i++) {
if (feed.indexOf(" ") == 0 || feed.length() == 0) {
digested.append(swallowed + " ");
swallowed = new StringBuilder();
} else {
swallowed.insert(0, feed.charAt(0));
}
feed = (feed.length() > 0) ? feed.delete(0, 1) : feed ;
}
return digested;
}
run:
olleH dlroW sihT sI s'ynoT edoC
BUILD SUCCESSFUL (total time: 0 seconds)
Here's the simplest solution that doesn't even use any loops.
public class olleHdlroW {
static String reverse(String in, String out) {
return (in.isEmpty()) ? out :
(in.charAt(0) == ' ')
? out + ' ' + reverse(in.substring(1), "")
: reverse(in.substring(1), in.charAt(0) + out);
}
public static void main(String args[]) {
System.out.println(reverse("Hello World", ""));
}
}
Even if this is homework, feel free to copy it and submit it as your own. You'll either get an extra credit (if you can explain how it works) or get caught for plagiarism (if you can't).
Obviously:
if( input.equals("Hello world") ){
System.out.println("olleH dlrow");
}
Use a for-loop to get each character in the string as you need them, and collect them in a StringBuffer with the add() method.
Know your libraries ;-)
import org.apache.commons.lang.StringUtils;
String reverseWords(String sentence) {
return StringUtils.reverseDelimited(StringUtils.reverse(sentence), ' ');
}
Taking into account that the separator can be more than one space/tab and that we want to preserve them:
public static String reverse(String string)
{
StringBuilder sb = new StringBuilder(string.length());
StringBuilder wsb = new StringBuilder(string.length());
for (int i = 0; i < string.length(); i++)
{
char c = string.charAt(i);
if (c == '\t' || c == ' ')
{
if (wsb.length() > 0)
{
sb.append(wsb.reverse().toString());
wsb = new StringBuilder(string.length() - sb.length());
}
sb.append(c);
}
else
{
wsb.append(c);
}
}
if (wsb.length() > 0)
{
sb.append(wsb.reverse().toString());
}
return sb.toString();
}
I'm assuming you could just print the results (you just said 'the output should be...') ;-)
String str = "Hello World";
for (String word : str.split(" "))
reverse(word);
void reverse(String s) {
for (int idx = s.length() - 1; idx >= 0; idx--)
System.out.println(s.charAt(idx));
}
Or returning the reversed String:
String str = "Hello World";
StringBuilder reversed = new StringBuilder();
for (String word : str.split(" ")) {
reversed.append(reverse(word));
reversed.append(' ');
}
System.out.println(reversed);
String reverse(String s) {
StringBuilder b = new StringBuilder();
for (int idx = s.length() - 1; idx >= 0; idx--)
b.append(s.charAt(idx));
return b.toString();
}