Hi there, this is an assignment that I am working on...
Untitled.jpg
this is my solution but for some UNLOGICAL REASON THE CODE DOESN'T MANAGE ANYTHING BEYOND 6 LETTERS... :S :S :S
for instance try
encoding("encrypt" , {1,4,3}, {3,2,4}); // now remove the t and run it, it works just fine :S
BTW StackObj and QueueObj are stack of Objects and Queue of Objects...
any hint would be quite helpful thank you...
public static StackObj reverse(StackObj x){ int size = x.size(); StackObj temp1 = new StackObj(size); StackObj temp2 = new StackObj(size); while(!x.isEmpty()){ temp1.push(new Integer(((Integer)x.pop()).intValue())); } while(!temp1.isEmpty()){ temp2.push(new Integer(((Integer)temp1.pop()).intValue())); } while(!temp2.isEmpty()){ x.push(new Integer(((Integer)temp2.pop()).intValue())); } return x; } public static String encoding(String message, int[] key1, int[] key2){ QueueObj Qforkey1 = new QueueObj(key1.length); for(int i = 0; !Qforkey1.isFull() ; i++){ Qforkey1.enqueue(new Integer (key1[i])); } StackObj Sforkey2 = new StackObj(key2.length); StackObj tempSforkey2 = new StackObj(key2.length); for(int i = key2.length-1; !Sforkey2.isFull() ; i--){ Sforkey2.push(new Integer(key2[i])); tempSforkey2.push(new Integer(key2[i])); } String EncodedMessage = ""; for(int i = 0; i < message.length(); i++){ int x = ((Integer)Qforkey1.dequeue()).intValue(); int y = ((Integer)Sforkey2.pop()).intValue(); int offset = x + y; Qforkey1.enqueue(new Integer (x)); if(Sforkey2.isEmpty()){ Sforkey2 = reverse(tempSforkey2); } if((message.charAt(i) >= 97) && (message.charAt(i) <= 122)){ int encryptedchar = message.charAt(i) + offset; if(encryptedchar > 122){ encryptedchar -= 122; encryptedchar += 96; } EncodedMessage += (char)encryptedchar; }else{ EncodedMessage += message.charAt(i); } } return EncodedMessage; }