TheAlgorithms-C-Plus-Plus/others/palindrome_of_number.cpp

37 lines
780 B
C++
Raw Normal View History

2020-05-28 23:24:36 +08:00
/**
* @file
* @brief Check if a number is
* [palindrome](https://en.wikipedia.org/wiki/Palindrome) or not.
*
* This program cheats by using the STL library's std::reverse function.
*/
2019-02-10 14:40:29 +08:00
#include <algorithm>
2020-05-26 21:26:40 +08:00
#include <iostream>
2019-02-10 14:40:29 +08:00
#ifdef _MSC_VER
// Required to compile std::toString function using MSVC
#include <string>
#else
#include <cstring>
#endif
2020-05-28 23:24:36 +08:00
/** Main function */
int main()
{
2020-05-26 21:26:40 +08:00
int num;
std::cout << "Enter number = ";
std::cin >> num;
2020-05-28 23:24:36 +08:00
std::string s1 = std::to_string(num); // convert number to string
2020-05-26 21:26:40 +08:00
std::string s2 = s1;
2020-05-28 23:24:36 +08:00
std::reverse(s1.begin(), s1.end()); // reverse the string
2020-05-28 23:24:36 +08:00
if (s1 == s2) // check if reverse and original string are identical
2020-05-26 21:26:40 +08:00
std::cout << "true";
else
std::cout << "false";
2020-05-26 21:26:40 +08:00
return 0;
}