Replace all $#$ in string by space in java


In this core java programming tutorial we will write a program to Replace $#$ in string by space in java.



Write a program to Replace $#$ in string by space.

entered string = java$#$made$#$so$#$easy
output string  = java made so easy



Must read: Sort array using merge sort in java.


Example/ Full Program/SourceCode to Replace $#$ in string by space in java >
/** Copyright (c), AnkitMittal  JavaMadeSoEasy.com */
public class ReplaceSpaceExample {
   public static void main(String[] args) {
         
          String input="java made so easy";
          System.out.println("entered string = "+input);
          System.out.println("output string  = "+replaceSpaces(input));
         
   }
   /*
   * Method replaces space with '$#$' in java
   */
   public static String replaceSpaces(String input) {
          char ch[]=input.toCharArray();
          int spaceCount = 0;
          for (int i = 0; i < ch.length; i++) {
                 if (ch[i] == ' ') {
                       spaceCount++;
                 }
          }
         
          int newLength = ch.length + spaceCount * 2;
          char chNew[]=new char[newLength] ;
          for (int i = ch.length - 1; i >= 0; i--) {
                 if (ch[i] == ' ') {
                       chNew[--newLength] = '$';
                       chNew[--newLength] = '#';
                       chNew[--newLength] = '$';
                 }
                 else
                       chNew[--newLength] = ch[i];
                
          }
          return(new String(chNew));
   }
}
/*OUTPUT
entered string = java made so easy
output string  = java$#$made$#$so$#$easy
*/


So In this core java programming tutorial we wrote a program how to Replace $#$ in string by space in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.




RELATED LINKS>









eEdit
Must read for you :