i am using StringBuilder function setCharAt(), have 2 variables start and end, everytime start get increased by one and end decreased by one.
sb.setCharAt(start , str.charAt(end))
Plz help to find error in the code,
i am using StringBuilder function setCharAt(), have 2 variables start and end, everytime start get increased by one and end decreased by one.
sb.setCharAt(start , str.charAt(end))
Plz help to find error in the code,
Hi himanshu
The logic you have applied is wrong. Let me explain how.
You have used the String Builder class along with a start and end index. What you wanted to do was probably reverse the string and check is its the same string(palindrome) or not.
For string “abcd”,i = 0, start = 0, end = 3
sb.setCharAt(start ,str.charAt(end));
will set “d” at the position of “a”. So the string after this step would be “dbcd”. This is the error. you are not storing the start character and overwriting the end character on it, leading to error. To rectify it use a temp variable and store the charAt(start) (like how you swap numbers you use a temp variable).
char t = sb.charAt(start);
sb.setCharAt(start ,str.charAt(end));
sb.setCharAt(end ,t);