Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 9 January 2017

Write a Java Program To Reverse The String With Preserving The Position Of Spaces ?

Write a Java Program to reverse string with preserving the spaces.For Example suppose we have String "I Love Java" then output must be "a vaJe voLI".

Example : 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class ReverseStringWithPreservingWhiteSpace {
 
 public static void getReverse(String str){
  
  char[] resultArray = new char[str.length()];
  
  for(int i=0;i<str.length();i++){
   if(str.charAt(i) == ' '){
    resultArray[i] = ' ';
   }
  }
  int n = resultArray.length - 1;
  
  for(int i=0;i<str.length();i++){
   if(str.charAt(i) != ' '){
    if(resultArray[n] == ' '){
     n--;
    }
    resultArray[n] = str.charAt(i);
    n--;
   }
  }
  System.out.println(str+" :- "+String.valueOf(resultArray));
 }
 public static void main(String[] args) {
  getReverse("Java is strongly Typed Language");
  getReverse("I Love Java");
 }
}

Output : 

Java is strongly Typed Language :- egau gn aLdepyTy lgnor tssiavaJ
I Love Java :- a vaJe voLI


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

2 comments:

  1. Thanks for sharing informative article on java application development. Your post helped to understand the career in Java. JAVA Training in Chennai

    ReplyDelete