0

Write a program to accept a number and check wether it is palindrome or not in for loop?

for loop

14th Sep 2017, 2:32 PM
Manas
3 Antworten
+ 2
[Suggestion] Go to Code Playground section of the app, type "palindrome" on the search bar (upper left corner, next to a,magnifier icon) and from the drop-down menu to its right, select "Most Popular", you'll see so many people's code to learn from. Hth, cmiiw
14th Sep 2017, 4:06 PM
Ipang
+ 3
Go through half the string, instead of the entire thing. If the right half is equal to the left half, it's a palindrome. Lets use an example String, say: "CoAoC" 'C' and 'C' are equal. 'o' and 'o' are equal. 'A' doesn't matter cause it's in the middle. Therefore, this string is a palindrome. This is a more effecient algorithm. This is an example using Java: static boolean isPalindrome(String myString){ // myString is the String to check for(int i = 0; i < myString.length() / 2; i++) if(myString.charAt(i) != myString.charAt(myString.length() - i - 1) return false; return true; }
14th Sep 2017, 3:18 PM
Rrestoring faith
Rrestoring faith - avatar
0
Does not work on SoloLearn compiler because of this: https://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-g-mingw #include<string> #include<algorithm> bool isPalindrome(unsigned n) { auto str { std::to_string(n) }; auto rstr { str }; std::reverse(rstr.begin(), rstr.end()); return str == rstr; }
14th Sep 2017, 2:41 PM
Testing003