Menu Bar

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 !!

4 comments:

  1. Sas Training Institute in Noida-Webtrackker is the best sas training institute in noida. SAS has taken the lead role for a long time, where most companies have standard software. While you have this certification under your belt, give big rewards to the IT industry, it can also serve as an important payer on the business side of the economy. SAS can read data files created by other statistical packages. Therefore, for experienced users of these statistical packages, SAS does not threaten to create data files created by these packages in a SAS file format.
    Sap Training Institute in Noida
    PHP Training Institute in Noida
    Hadoop Training Institute in Noida
    Oracle Training Institute in Noida
    Linux Training Institute in Noida
    Dot net Training Institute in Noida
    Salesforce training institute in noida
    Java training institute in noida

    ReplyDelete