Wednesday 3 August 2016

zipZap

Home
Goto Problem

Look for patterns like "zip" and "zap" in the string -- length-3, starting with 'z' and ending with 'p'. Return a string where for all such words, the middle letter is gone, so "zipXzap" yields "zpXzp".

zipZap("zipXzap") → "zpXzp"
zipZap("zopzop") → "zpzp"
zipZap("zzzopzop") → "zzzpzp"

public String zipZap(String str)
{
  String temp=str,res="";
  int j=temp.length();
  int i=0;
  while(i<j-2)
  {
      if(temp.charAt(i)=='z' && temp.charAt(i+2)=='p')
     {
         res=res+temp.substring(0,i+1);
         temp=temp.substring(i+2);
         j=temp.length();
         i=0;
      }
     i++;
  }
res=res+temp; return res;
}

No comments:

Post a Comment