The Artima Developer Community
Sponsored Link

Java Buzz Forum
Reverse words in a String

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Reverse words in a String Posted: Nov 9, 2015 12:22 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Reverse words in a String
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1. Java Interview Program to Reverse words in a string


  1. package com.instaceofjava;
  2.  
  3. public class ReverseString {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String strng= "Instance of Java ";
  8.  
  9. String str[] =strng.split(" ");
  10.  
  11. String result="";
  12.  
  13. for(int i=str.length()-1;i>=0;i--){
  14.  
  15. result += str[i]+" ";
  16.  
  17. }
  18.  
  19. System.out.println(result );
  20. }
  21. }
Output:

  1. Java of Instance

2. Java Interview Program to Reverse words in a string


  1. package com.instaceofjava;
  2.  
  3. public class ReverseString {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String strng= "Instance of Java ";
  8.  
  9. StringBuilder sb = new StringBuilder(strng.length() + 1);
  10.  
  11.   String[] words = strng.split(" ");
  12.  
  13.   for (int i = words.length - 1; i >= 0; i--) {
  14.          sb.append(words[i]).append(' ');
  15.    }
  16.     
  17.     sb.setLength(sb.length() - 1); 
  18.  
  19.    String result= sb.toString();  
  20.  
  21.     System.out.println(result);

  22. }
  23. }
Output:

  1. Java of Instance

Read: Reverse words in a String

Topic: 2 new photos uploaded to flickr Previous Topic   Next Topic Topic: The strategy burden and Electronic Marginalia – Software Defined Talk #48

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use