please help me to find error
code link:https://ide.codingblocks.com/s/101533
Can't get error here
while (i<str.length() && str.charAt(i) == ch )
The only error is that you have to add this first condition (in bold) in the while loop. Because during execution when the while loop breaks, āiā exceeds str.length()-1 and the index goes out of bound. So it will give runtime error in some cases.
I am able to submit the code on hackerblocks and pass all test cases after making the above change. Did you try to submit? If it is not running, try copying the sample input and pasting it in the input box again, or refresh.
The problem is not with the code. You can check the following:
- Have you made the required change to the code? Make sure you put these conditions in this order only otherwise it will give runtime error
- Have you chosen the correct language for the ide while submitting?
@CRX18Aug0044
why order matters here
while ( i<str.length() && str.charAt(i) == ch )
because and operator will check both the conditions
And operator checks both conditions but order matters here because if (str.charAt(i) == ch) condition is checked first and āiā and is an out of bound index i.e greater than length of string, then an error will be thrown immediately without the next condition being checked. So, first you have to check if āiā is a valid index and then check the other condition
Please mark the doubt as resolved if you have no further queries!