This is what I have:
class encoded
{
public static void main(String[] args)
{
String s1 = \"hello\";
char[] ch = s1.toCharArray();
Replace i - 'a' + 1
with ch[i] - 'a' + 1
class encoded {
public static void main(String[] args)
{
String s1 = "hello";
char[] ch = s1.toCharArray();
for(int i=0;i<ch.length;i++)
{
char c = (char) (((ch[i] - 'a' + 1) % 26) + 'a');
System.out.print(c);
}
}
}
You are using the loop index i
instead of the i
th character in your loop, which means the output of your code does not depend the input String
(well, except for the length of the output, which is the same as the length of the input).
Change
char c = (char) (((i - 'a' + 1) % 26) + 'a');
to
char c = (char) (((ch[i] - 'a' + 1) % 26) + 'a');