0
Write a program to accept a number and check wether it is palindrome or not in for loop?
for loop
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
+ 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;
}
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;
}