Friday, 17 July 2015

Java Programming

Q)..String encrption. replace the odd-index character with next character(if it is 'z' replace with 'a'..if 'a' with 'b' as such),
leave the even index as such. return the encrypted string.



  1.  
  2. package Set1;
  3. public class ClassSet37 {
  4. public static void main(String[] args) {
  5.       String s="preethi";
  6.       System.out.println(oddEncryptionOfString(s));
  7.       }
  8. public static String oddEncryptionOfString(String s) {
  9.       StringBuffer sb=new StringBuffer();
  10.       for(int i=0;i<s.length();i++){
  11.             char c=s.charAt(i);
  12.             if(c%2!=0){
  13.                   c=(char)(c+1);
  14.                   sb.append(c);  }
  15.             else
  16.                   sb.append(c);   }
  17.       return sb.toString();
  18. }
  19. }