Saturday, 7 September 2013

java program to replace the spaces in a string by '%20'.

Here is the code ....


       

public class P15 {
 public static void main(String args[]) {
  String name = "gaurav kumar yadav";
  int val = 0;
  for (int i = 0; i < name.length(); i++)
   if (name.charAt(i) == ' ') val = val + 1;

  char[] chr = new char[name.length() + 2 * val];
  for (int l = 0, s = 0; l < name.length(); l++) {
   if (name.charAt(l) != ' ') {
    chr[s] = name.charAt(l);
    s++;
   } else {
    chr[s] = '%';
    chr[++s] = '2';
    chr[++s] = '0';
    s++;
   }

  }
  String name1 = new String(chr);
  System.out.println(name1);
 }
}

       
 

No comments:

Post a Comment