arsovski

is_word_palindrom

Aug 29th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. //TODO Tuj vasko shto ti gu prevede zadachu
  2.  
  3. #include<iostream>
  4. #include "str_len.h"
  5. #include "str_match.h"
  6.  
  7. char* revert_string(char* word)
  8. {
  9.     int size_word = str_len(word);
  10.  
  11.     char* reverted_string = new char[size_word];
  12.  
  13.     for(int i=size_word-1,j=0;i>=0;i--,j++)
  14.     {
  15.         reverted_string[j] = word[i];
  16.     }
  17.  
  18.  
  19.     reverted_string[size_word] = '\0';
  20.  
  21.     return reverted_string;
  22. }
  23.  
  24. //used for checking the terminating zero
  25.  
  26. void print_string(char* str)
  27. {
  28.     while(*str !='\0')
  29.     {
  30.         std::cout << *str;
  31.         str++;
  32.     }
  33.  
  34.     std::cout << std::endl;
  35. }
  36.  
  37.  
  38. bool isPalindrom(char* word)
  39. {
  40.     const int LEN_WORD = str_len(word);
  41.  
  42.     if (LEN_WORD % 2 != 0)
  43.         return false;
  44.  
  45.  
  46.     char* reverted_word = new char[LEN_WORD];
  47.     reverted_word = revert_string(word);
  48.  
  49.     return str_match(word, reverted_word);
  50. }
  51.  
  52.  
  53. int main()
  54. {
  55.  
  56.     char word[] = "DAAAAD";
  57.  
  58.     if (isPalindrom(word))
  59.     {
  60.         std::cout << "True " << std::endl;
  61.     }
  62.     else
  63.         std::cout << "False " << std::endl;
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment