Friday 17 October 2014

java programming problem

Question:
Replace the characters  in a string with the space
example;
Replace %20 with ' ' in the following text
input:
www.space%20.com
output:
www.space  .com


solution:

you just have to check each and every character of the given string .If any character matched with the given character(here %,2,0) replace that with space.

program:

package com.shashi;

public class ChangingData {

    public static void main(String []args)
    {
        String input="www.space%20.com";
        StringBuffer buff=new StringBuffer();
        for(int i=0;i<input.length();i++)
        {
            if(input.charAt(i)=='%' && input.charAt(i+1)=='2' && input.charAt(i+2)=='0')
            {
                //do nothing
            }
            else if(input.charAt(i)=='2' && input.charAt(i+1)=='0')
            {

                //do nothing
            }
            else if(input.charAt(i)=='0')
            {

                buff.append(' ');
            }
            else
                buff.append(input.charAt(i));
        }
        System.out.print(buff);
        



       
    }
}


output:
www.space .com









.................................................
Note:this program is complied and runned under jdk1.8




No comments: