Wednesday, 15 July 2015

Java Programs Made Easy

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