1

newer programmer here, having trouble diagnosing the issue with my string reversal program. The program is supposed to compare two strings and find out if string x is the reversal of string y and if it is it returns true, if not it returns false.

public class Reverse
{
   public static void main(String[] args)
   {

      System.out.println(isExactReverse("ba", "a"));
      System.out.println(isExactReverse("desserts", "stressed"));
      System.out.println(isExactReverse("apple", "apple"));
      System.out.println(isExactReverse("regal", "lager"));
      System.out.println(isExactReverse("war", "raw"));
      System.out.println(isExactReverse("pal", "slap"));


   }
   public static boolean isExactReverse(String x, String y)
   {
      //To be completed
      int counter = 0;
      int temp = x.length() - 1;
      boolean result = false;

      for(int i = 0; i < y.length(); i++)
      {
         if(x.charAt(temp) == y.charAt(i))
         {
            counter++;
         }    
         temp--;
      }
      if(counter == y.length())
      {
        result = true; 
      }

    return result;
     }

}

the output i get is not correct and I am getting a runtime error.

true
true
false
true
true


Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:658)
    at Reverse.isExactReverse(Reverse.java:24)
    at Reverse.main(Reverse.java:11)

Expected output:

False
True
False
True
True 
False
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
pro5476
  • 33
  • 1
  • 6

2 Answers2

4

The problem in your code is that it assumes that x and y have the same length. When it is not so, the code either returns a false positive, as in case of "ba"-"a", or crashes, as in case of "pal"-"slap".

Fix this by adding this check at the top of your isExactReverse method:

if (x.length() != y.length()) {
    return false;
}

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • @pro5476 No problem! If you are not looking for additional help with this question, please consider accepting the answer by clicking the check mark on the left. This would let other site visitors know that your problem is solved, and earn you a new badge on Stack Overflow. – Sergey Kalinichenko Feb 09 '18 at 20:02
0

The code is certainly able to be compiled. It would be nice if you could specify the problem. But I guess you got problems getting the correct result. First of all, I would recommend you simplified the code. There is a lot of lines, that don't have to be there. Here is, what I would do.

public class Reverse
{
    public static void main(String[] args)
    {
        System.out.println(isExactReverse("ba", "a"));
        System.out.println(isExactReverse("desserts", "stressed"));
        System.out.println(isExactReverse("apple", "apple"));
        System.out.println(isExactReverse("regal", "lager"));
        System.out.println(isExactReverse("war", "raw"));
        System.out.println(isExactReverse("pal", "slap"));
    }

    public static boolean isExactReverse(String x, String y)
    {
        //To be completed
        int temp = x.length() - 1;
        boolean result = false;

        for(int i = 0; i < y.length(); i++)
        {
            if(!x.charAt(temp).equals(y.charAt(i)))
            {
                return false;
            }    

            temp--;
        }

        return true;
    }
}

This should work. I don't really have an answer for your post, because I don't know you problem, but this is just some kind of help that maybe will resolve your problem.