cum scriu
#include <algorithm>#include <cctype>#include <functional>#include <string>bool palindrome( unsigned long n ) { unsigned long x = n; unsigned long u = 0; while (x) { u *= 10; u += x % 10; x /= 10; } return (n == u); }bool palindrome( std::string s ) { s.erase( std::remove_if( s.begin(), s.end(), std::not1( std::ptr_fun <int, int> ( std::isalnum ) ) ), s.end() ); std::transform( s.begin(), s.end(), s.begin(), std::ptr_fun <int, int> ( std::tolower ) ); std::string z = s; std::reverse( z.begin(), z.end() ); return (s == z); }#include <iostream>#include <limits>using namespace std;int main() { string s; unsigned long n; cout << "Palindrome tester\n"; cout << "Enter nothing to quit.\n"; while (true) { cout << "Enter a string> "; getline( cin, s ); if (s.empty()) break; cout << "Enter a number> "; cin >> n; if (!cin) cin.clear(); cin.ignore( numeric_limits <streamsize> ::max(), '\n' ); cout << "The string is " << (palindrome( s ) ? "" : "not ") << "a palindrome.\n"; cout << "The number is " << (palindrome( n ) ? "" : "not ") << "a palindrome.\n\n"; } cout << "Good bye.\n"; return 0; }
in pseudocod?
Răspunsuri la întrebare
// Obtaining the last digit of your number
int digit = n%10;
// "push_back"
number *= 10;
number += digit;
// "pop_back
number /= 10;
#include <iostream>
using namespace std;
int palindrome_test(int palindrome_check);
int main()
{
int number;
cout << "Give me an integer.\n";
cin >> number;
cout << "That number has " << palindrome_test(number) << " digits.\n";
}
int palindrome_test(int palindrome_check)
{
bool palindrome_bool;
int size;
int palindrome_number[size];
int digits;
int digit_check;
digit_check = palindrome_check;
do
{
digit_check = digit_check / 10;
digits++;
} while (digit_check >= 0);
return digits;
}
bool isPalindrome(const std::string& s)
{
int size = s.size();
for(int i = 0; i < size / 2; ++i)
{
if(s[i] != s[size - 1 - i]) return false;
}
return true;
}
bool palindrome(int x){
int t = x, m = 0;
do m = m*10 + t%10;
while(t /= 10);
return m == x;
}
bool isPalindrome(int value)
{
std::stringstream buff;
buff << value;
return isPalindrome(buff.str());
}
bool isPalindrome(std::string toCheck)
{
for(int i = 0, j = toCheck.length() - 1; i < j; ++i, --j)
{
if(toCheck[i] != toCheck[j])
{
return false;
}
}
return true;
}
#include <algorithm>
#include <cctype>
#include <functional>
#include <string>
bool palindrome( unsigned long n )
{
unsigned long x = n;
unsigned long u = 0;
while (x)
{
u *= 10;
u += x % 10;
x /= 10;
}
return (n == u);
}
bool palindrome( std::string s )
{
s.erase(
std::remove_if(
s.begin(),
s.end(),
std::not1( std::ptr_fun <int, int> ( std::isalnum ) )
),
s.end()
);
std::transform( s.begin(), s.end(), s.begin(), std::ptr_fun <int, int> ( std::tolower ) );
std::string z = s;
std::reverse( z.begin(), z.end() );
return (s == z);
}
#include <iostream>
#include <limits>
using namespace std;
int main()
{
string s;
unsigned long n;
cout << "Palindrome tester\n";
cout << "Enter nothing to quit.\n";
while (true)
{
cout << "Enter a string> ";
getline( cin, s );
if (s.empty()) break;
cout << "Enter a number> ";
cin >> n;
if (!cin) cin.clear();
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
cout << "The string is " << (palindrome( s ) ? "" : "not ") << "a palindrome.\n";
cout << "The number is " << (palindrome( n ) ? "" : "not ") << "a palindrome.\n\n";
}
cout << "Good bye.\n";
return 0;
}
int main()
{
int number;
int digits_a;
cout << "Give me an integer.\n";
cin >> number;
do
{
number = number / 10;
digits_a++;
} while (number != 0);
cout << "That number has " << digits_a << " digits.\n";
}
Edit & Run
#include <iostream>
using namespace std;
bool palindrome_test(int isPalindrome[], int size);
int digit_count(int x);
int main()
{
int number;
int length = 0;
int palindrome[length];
bool Palindrome_check;
cout << "Please input an integer.\n";
cin >> number;
length = digit_count(number) - 1;
palindrome[length] = number;
Palindrome_check = palindrome_test(palindrome[length],length);
if (Palindrome_check = true)
{
cout << "That number is a palindrome\n";
}
else
{
cout << "That number is not a palindrome\n;";
}
}
int digit_count(int x)
{
int digits = 0;
do
{
x /= 10;
digits++;
} while (x != 0);
return digits;
}
bool palindrome_test(int isPalindrome[], int size)
{
int f = 0; //first number
int l = size; //last number
int count;
for (count = 0; count <= size; count++)
{
if (f == l)
{
f++;
l--;
}
else
{
return false;
}
}
return true;
}
#include <iostream>
using namespace std;
bool palindrome(int x);
int main()
{
int number;
bool Palindrome_check;
cout << "Please input an integer.\n";
cin >> number;
Palindrome_check = palindrome(number);
if (Palindrome_check == true)
{
cout << "That number is a palindrome\n";
}
else
{
cout << "That number is not a palindrome\n";
}
}
bool palindrome(int x)
{
int t = x, m = 0;
do
{
m = m*10 + t%10;
} while(t /= 10);
return m == x;
}
Edit & Run
bool palindrome_test(int isPalindrome[], int size);
bool palindrome_test(int *isPalindrome, int size); // note that int *
Palindrome_check = palindrome_test(palindrome,length); // 'palindrome' points to the begin of the array