• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to reverse words in line  RSS feed

 
krishna chandra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
how to reverse words in aline
eg; i love india

output should be india love i

Thanks,
krishan
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by krishna chandra:
hi,
how to reverse words in aline
eg; i love india

output should be india love i

Thanks,
krishan


Use String.split() to separate the line into words and then append the contents of the array together starting with the last element, going thru to the first and put a space between each word.
 
Sachin Ramesh Vir
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be it will useful to u!!

 
Robert Watkins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:


Use String.split() to separate the line into words and then append the contents of the array together starting with the last element, going thru to the first and put a space between each word.


Personally, I'd create a list from the array, then use Collections.reverse()... it's a little more obvious as to what you are doing compared with a decreasing counter in a for loop (which always looks odd to me)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!