How do i make an array with the opposite letters of the first array im making? For example if the string is "Hello", i want to use arrays to print out "olleH". When i try to return the variables it tells me "String index out of range: -1". Can anyone please tell me why? This is my code so far:
public class april{
public static void main(String [] args){
System.out.println("What do you want backwards?");
System.out.println("Your new word is " + reverse(IO.readString()));
}
public static String reverse(String original){
char [] letters = new char [original.length()];
char [] opp = new char [original.length()];
char c= 'a';
char d= 'a';
String word= " ";
String opposite= " ";
for (int x=0;x<original.length();x++){
c = original.charAt(x);
letters[x]= c;
if (x!=0){
d = original.charAt(-x-1);
opp[-x]=d;
}
else if (x==0){
d = original.charAt(-1);
opp[x]= d;
}
word += letters[x];
opposite += opp[x];
}
return word;
return opposite;