write a program to enter a string,a position where you want to change a character and a character now replace it with given position.

class ReplaceStringCharactor
{
 public static void main(String args[])
 {
  String str="apple";
  char oldchar='p';
  char newchar='b';
  int len=str.length();
  char[] a=new char[len];
  str.getChars(0, len, a, 0);
  System.out.println("string before replace   :"+str);
  int i=0;
  while(i<len)
  {
    if(a[i]==oldchar)
     {
      a[i]=newchar;
     }
   i++;
  }
 System.out.print("String after replace    :");
 String newstr=new String(a);
 System.out.println(newstr);
 }
}

1 comment: