In: Computer Science
I have been working on this assignment in Java programming and can not get it to work.
This method attempts to DECODES an ENCODED string without the
key.
public static void breakCodeCipher(String plainText){
char input[]plainText.toCharArray();
for(int j=0; j<25; j++){
for(int i=0; i<input.length; i++)
if(input[i]>='a' && input[i]<='Z')
input[i]=(char)('a'+((input[i]-'a')+25)%26);
else if(input[i]>='A'&& input[i]<='Z')
input[i]=(char)('A'+ ((input[i]-'A')+25)%26);
}
System.out.println(plainText)
}
There are a few compilation and semantic errors that I've seen
in your code. They are:
-> input[] isn't initialised.
-> In the second loop '{}' aren't used, which makes the compiler
think that it has to execute only the line next tot the loop.
-> The print statement doesnot have a semicolon.
-> Finally, you are printing the plaintext but not the decoded
string.
I've made these fixes and included the code and output for
you.
Code:
public class A
{
public static void breakCodeCipher(String
plainText)
{
char input[] =
plainText.toCharArray();
for(int j=0; j<25; j++)
{
for(int i=0;
i<input.length; i++)
{
if(input[i]>='a' &&
input[i]<='Z')
input[i]=(char)('a'+((input[i]-'a')+25)%26);
else if(input[i]>='A'&&
input[i]<='Z')
input[i]=(char)('A'+
((input[i]-'A')+25)%26);
}
}
System.out.println(input);
}
public static void main(String args[])
{
breakCodeCipher("ZABCDE");
}
}
Output: