Advertisement
BenjaminWade

bard

May 11th, 2023
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | Software | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <locale.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6.  
  7. int is_it_palindrome(const char* str)
  8. {
  9.     int lenght;
  10.     int has_alnum = 0;
  11.     lenght = strlen(str);
  12.  
  13.     const char* start = str + 0;
  14.     const char* end = str + lenght - 1;
  15.  
  16.     while (start <= end)
  17.     {
  18.         if (!isalnum(*start))
  19.         {
  20.             start++;
  21.         }
  22.         else if (!isalnum(*end))
  23.         {
  24.             end--;
  25.         }
  26.         else if (toupper(*start) == toupper(*end))
  27.         {
  28.             has_alnum = 1;
  29.             start++;
  30.             end--;
  31.         }
  32.         else
  33.         {
  34.             return 0;
  35.         }
  36.     }
  37.  
  38.     return has_alnum;
  39. }
  40.  
  41.  
  42.  
  43. int longest_palindrome(char *word) {
  44.     int n = strlen(word);
  45.     int ehpalindromo = is_it_palindrome(word);
  46.     if (ehpalindromo == 1)
  47.     {
  48.         return n;
  49.     }
  50.  
  51.   int i, j, longest_palindrome_length = 0, longest_palindrome_index = 0;
  52.  
  53.   for (i = 0; word[i] != '\0'; i++) {
  54.     for (j = i + 1; word[j] != '\0'; j++) {
  55.       int is_palindrome = 1;
  56.       for (int k = i; k < j; k++) {
  57.         if (word[k] != word[j - k + i - 1]) {
  58.           is_palindrome = 0;
  59.           break;
  60.         }
  61.       }
  62.  
  63.       if (is_palindrome && j - i > longest_palindrome_length) {
  64.         longest_palindrome_length = j - i;
  65.         longest_palindrome_index = i;
  66.       }
  67.     }
  68.   }
  69.  
  70.   return longest_palindrome_length;
  71. }
  72.  
  73. int main() {
  74.   char word[100];
  75.   int longest_palindrome_length;
  76.  
  77.   printf("Enter a word: ");
  78.   scanf("%s", word);
  79.  
  80.   longest_palindrome_length = longest_palindrome(word);
  81.   printf("The longest palindrome in the word '%s' is of length %d.\n", word, longest_palindrome_length);
  82.  
  83.   return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement