Please help me find out where I am going wrong. Thanks. public class rot13a { pu
ID: 3621619 • Letter: P
Question
Please help me find out where I am going wrong. Thanks.
public class rot13a
{
public static char[] cipher (char x[], int n)
{
if (n==0){
if (x[n] >= 'a' && x[n] <= 'm') x[n] += 13;
else if (x[n] >= 'n' && x[n] <= 'z') x[n] -= 13;
else if (x[n] >= 'A' && x[n] <= 'M') x[n] += 13;
else if (x[n] >= 'N' && x[n] <= 'Z') x[n] -= 13;
return x;
}
else{
if (x[n] >= 'a' && x[n] <= 'm') x[n] += 13;
else if (x[n] >= 'n' && x[n] <= 'z') x[n] -= 13;
else if (x[n] >= 'A' && x[n] <= 'M') x[n] += 13;
else if (x[n] >= 'N' && x[n] <= 'Z') x[n] -= 13;
return cipher(x,n-1);
}
}
public static void main (String args[])
{
char str[]=ITI1120.readCharLine();
int n=str.length;
char a[];
a = cipher(str, n);
System.out.println(a[0]);
}
}
Explanation / Answer
please rate - thanks
your problem is that n is the length of the string, the indices going from 0 to n-1. you are sending the function n which is too big it should be n-1.
I would have done more on the code but I don't know what it does. and since your read c haracter I changed the array initialization, and I changed it to print the entire array when done
hope this helps
public class rot13a
{
public static char[] cipher (char x[], int n)
{
if (n==0){
if (x[n] >= 'a' && x[n] <= 'm') x[n] += 13;
else if (x[n] >= 'n' && x[n] <= 'z') x[n] -= 13;
else if (x[n] >= 'A' && x[n] <= 'M') x[n] += 13;
else if (x[n] >= 'N' && x[n] <= 'Z') x[n] -= 13;
return x;
}
else{
if (x[n] >= 'a' && x[n] <= 'm') x[n] += 13;
else if (x[n] >= 'n' && x[n] <= 'z') x[n] -= 13;
else if (x[n] >= 'A' && x[n] <= 'M') x[n] += 13;
else if (x[n] >= 'N' && x[n] <= 'Z') x[n] -= 13;
return cipher(x,n-1);
}
}
public static void main (String args[])
{
char str[]={'s','t','r','i','n','g'};
int n=str.length;
char a[];
a = cipher(str, n-1);
for(int i=0;i<n;i++)
System.out.print(a[i]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.