473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

reverse each word of the String

Hello
I want to do reverse each word of the String
example if input String is "Hello World" then the output should be "olleH dlroW"

i Have tried this far
Expand|Select|Wrap|Line Numbers
  1. class g
  2. {
  3. static String s="Hello World Hello World";
  4. static int last=s.lastIndexOf(' ');
  5. static String tempS;
  6.  
  7. public static void start(int count)throws Exception
  8. {
  9. if(count!=last)
  10. {
  11. tempS+=reverse(s.substring(count,s.indexOf(count+1,' ')));
  12. start(s.indexOf(count+1,' '));
  13. }
  14.  
  15. if (count==last)
  16. {
  17. tempS+=reverse(s.substring(count));
  18. System.out.println( tempS);
  19. }
  20. }
  21.  
  22. public static String reverse(String s1)
  23. {
  24. s1.trim();
  25. int l=s1.length();
  26. String temp=null;
  27. for(int i=l;i>0;i++)
  28. {
  29. temp+=s1.charAt(i);
  30. }
  31. temp+=" ";
  32. return temp;
  33. }
  34.  
  35. public static void main()throws Exception
  36. {
  37. start(0);
  38. }
  39. }
Nov 26 '08 #1
3 7073
JosAH
11,448 Expert 8TB
Have a look at the StringBuilder class; it can do the work for you.

kind regards,

Jos
Nov 26 '08 #2
jkmyoung
2,057 Expert 2GB
Line 26. String temp=null;
This should be String temp = new String(); //empty string

You can't add characters to a null, but you can add them to an empty string.
Nov 26 '08 #3
JosAH
11,448 Expert 8TB
@jkmyoung
This works for me:

Expand|Select|Wrap|Line Numbers
  1. public class AddToNull {
  2.  
  3.     public static void main (String[] args) {
  4.  
  5.         String s= null+"abc";
  6.         System.out.println(s);
  7.     }
  8. }
kind regards,

Jos ;-)
Nov 26 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: tjland | last post by:
Okay so im working on a very simple encryption method using just loops. Kind of novel i think. Okay so first i set up a list of the alphabet with just every seperate letter, then user is prompted...
59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
14
by: ford_desperado | last post by:
Why isn't ALLOW REVERSE SCANS the default? Why do we have to - drop PK - create an index - recreate PK What are the advantages of indexes that do not allow reverse scans?
8
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
1
by: Jerry | last post by:
Hi, I want to draw a reverse string, does anyone know how to implement it? And I want to draw a vertical string, it can be implemented by setting StringFormat.StringFormatFlags...
47
by: sudharsan | last post by:
could any one please give me a code to reverse a string of more than 1MB .??? Thanks in advance
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
40
by: KG | last post by:
Could any one tell me how to reverse the bits in an interger?
2
by: alireza6485 | last post by:
Hello every one: I have to write a program that reverse and displays the charactors of a pharase typed from the keyboard. This is what I wrote: using System; public class YourName { public...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.