-3

I'm pretty sure that my logic is right but it doesn't print anything. Can anyone please take a look at my code. It supposes to print the string backward. But it doesn't print anything. It doesn't give me an error either. Thanks

String x = input.nextLine();

    for(int i = x.length()-1; i<=0;i--)
   {
   System.out.println(x.charAt(i));

   }

1 Answers1

1

<= vs >=. For backward you loop should check >=

  for(int i = x.length()-1; i<=0;i--)
   {
   System.out.println(x.charAt(i));

   }

Should be

  for(int i = x.length()-1; i>=0;i--)
   {
   System.out.println(x.charAt(i));

   }
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307