mdgaziur001

Help thing :)

Aug 31st, 2021 (edited)
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <ctype.h>
  5.  
  6. bool is_alpha(char ch) {
  7.     if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90)) {
  8.         return true;
  9.     }
  10.     return false;
  11. }
  12.  
  13. size_t getl(char **lineptr)
  14. {
  15.     size_t size = 0;
  16.     char ch;
  17.  
  18.     while ((ch = getchar()) != '\n' && ch != EOF)
  19.     {
  20.         // allocate it if it's not
  21.         if (!*lineptr) {
  22.             *lineptr = (char*)malloc(sizeof(char));
  23.             size = 1;
  24.         }
  25.         else {
  26.             *lineptr = (char*)realloc(*lineptr, ++size);
  27.         }
  28.  
  29.         // copy
  30.         (*lineptr)[size - 1] = ch;
  31.     }
  32.  
  33.     // add null termination if the pointer is not null
  34.     if (*lineptr) {
  35.         // realloc with +1 size
  36.         *lineptr = (char*)realloc(*lineptr, ++size);
  37.         (*lineptr)[size - 1] = '\0';
  38.     }
  39.  
  40.     return size - 1; // don't count null termination
  41. }
  42.  
  43. size_t filter_alphabets(char **target, size_t size) {
  44.     char *result = NULL;
  45.     size_t result_size = 0;
  46.     size_t idx = 0;
  47.  
  48.     while(idx < size) {
  49.         if(is_alpha((*target)[idx])) {
  50.             if(!result) {
  51.                 result = (char*)malloc(sizeof(char));
  52.                 result_size = 1;
  53.             }
  54.             else {
  55.                 // increase buffer size by one
  56.                 result = (char*)realloc(result, ++result_size);
  57.             }
  58.  
  59.             result[result_size - 1] = tolower((*target)[idx]);
  60.         }
  61.         idx++;
  62.     }
  63.  
  64.     // add null termination char if result is not null
  65.     if (result) {
  66.         result = (char*)realloc(result, ++result_size);
  67.         result[result_size - 1] = '\0';
  68.         free(*target);
  69.     } else {
  70.         result_size = 1; // fixes negative return thing. I'm so lazy
  71.     }
  72.  
  73.     *target = result;
  74.  
  75.     return result_size - 1; // ignore null termination
  76. }
  77.  
  78. bool is_palindrome(char *src, size_t size) {
  79.     size_t i = 0;
  80.     size_t j = size - 1;
  81.  
  82.     while(i < size) {
  83.         if(src[i] != src[j])
  84.             return false;
  85.         i++;
  86.         j--;
  87.     }
  88. }
  89.  
  90. int main()
  91. {
  92.     while(1) {
  93.         char *input = NULL;
  94.         size_t size = getl(&input);
  95.         if(!input)
  96.             // input is empty or EOF
  97.             break;
  98.  
  99.         // remove all non alphabets
  100.         size = filter_alphabets(&input, size);
  101.  
  102.         if(is_palindrome(input, size))
  103.             printf("Palindrome\n");
  104.         else
  105.             printf("Not Palindrome\n");
  106.  
  107.         free(input);
  108.     }
  109. }
Add Comment
Please, Sign In to add comment