An example of how to check whether a string is a palindrome or not using C+.+ Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!
here in if statement can u explain the indexing how we did it?? because if text length is 10-1-1==8,how we are comparing is it the correct way what I calculated
Great question Rakshith! Remember that i starts off at 0. So if the length is 10 then we get 10 - 1 - i which is 10 - 1 - 0 which is then 9. And that would mean we are comparing the element at index 0 with the element at index 9, which is exactly what we want because (due to zero-indexing) the last char in the string will be at index 9. :-)
couldn't you add another empty string, then with for loop from back add reversed text to this empty string and then check if they are equal? I mean it is working in c# and i am not sure if you can do this in c++.
Great question Urishita! 🙂 We want to compare the 1st element to the last element, the 2nd element to the 2nd last element and so on to see if all of the corresponding elements match. So we really only need our loop counter variable to go until the middle of the string. Once we reach the middle of the string, we will have compared the 1st element to the last, the 2nd element to the 2nd last element, and so on. We could go keep past the middle of the string, but we would just end up making the same comparisons again, so for example by the last run of the loop we would just e comparing the last element to the 1st element again. We save work by only checking up until the middle of the string as that is all that is required. 🙂