beetii

Palindrome

Jul 25th, 2021
882
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <new>
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. unsigned int len(const char* buff);
  8. char* makeString(const char* buff, const unsigned int& len);
  9. bool isPalindrome(const char* str, const int& checkF, const int& checkS);
  10.  
  11.  
  12. int main() {
  13.     char buff[256];
  14.  
  15.     cin >> buff;
  16.  
  17.     unsigned int length = len(buff);
  18.  
  19.     cout << "Length: " << length << endl;
  20.  
  21.     if (length == 0) {
  22.         cout << "Pali" << endl;
  23.     }
  24.     else {
  25.         char* str = makeString(buff, length);
  26.         if (isPalindrome(str, 0, length-1)) {
  27.             cout << "Pali" << endl;
  28.         }
  29.         else {
  30.             cout << "Not Pali" << endl;
  31.         }
  32.     }
  33.  
  34.  
  35.     return 0;
  36. }
  37.  
  38. bool isPalindrome(const char* str, const int& checkF, const int& checkS) {
  39.     if (checkF >= checkS) {
  40.         return true;
  41.     }
  42.  
  43.     if (str[checkF] != str[checkS]) {
  44.         return false;
  45.     }
  46.  
  47.     return isPalindrome(str, checkF + 1, checkS - 1);
  48. }
  49.  
  50. unsigned int len(const char* buff) {
  51.     unsigned int len = 0;
  52.  
  53.     while(*buff++) {
  54.         ++len;
  55.     }
  56.  
  57.     return len;
  58. }
  59.  
  60. char* makeString(const char* buff, const unsigned int& len) {
  61.     char* str = new char[len];
  62.  
  63.     for (unsigned int i = 0; i < len; ++i) {
  64.         str[i] = buff[i];
  65.     }
  66.  
  67.     return str;
  68. }
RAW Paste Data