C Program to Find the Largest & Smallest possible Word which is a Palindrome

This C Program finds the largest and smallest possible word which is a palindrome. A palindrome is a word that reads the same backward as forward, e.g., madam.

Here is a source code of the C program to find the largest and smallest possible word which is a palindrome. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Find the Largest & Smallest possible 
  3.  * Word which is a Palindrome
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8.  
  9. int palin_check(char []);
  10.  
  11. int main()
  12. {
  13.     char string[100], word[20], max[20], min[20], c;
  14.     int i = 0, j = 0, flag = 0, check;
  15.  
  16.     printf("Enter string: ");
  17.     i = 0;
  18.     do
  19.     {
  20.         fflush(stdin);
  21.         c = getchar();
  22.         string[i++] = c;
  23.  
  24.     } while (c != '\n');
  25.     string[i - 1] = '\0';
  26.     for (i = 0; i < strlen(string); i++)
  27.     {
  28.         while (i < strlen(string) && !isspace(string[i]) && isalnum(string[i]))
  29.         {
  30.             word[j++] = string[i++];
  31.         }
  32.         if (j != 0)
  33.         {
  34.             word[j] = '\0';
  35.             check = palin_check(word);
  36.             if (check)
  37.             {
  38.                 if (!flag)
  39.                 {
  40.                     flag = !flag;
  41.                     strcpy(max, word);
  42.                     strcpy(min, word);
  43.                 }
  44.                 if (strlen(word) > strlen(max))
  45.                 {
  46.                     strcpy(max, word);
  47.                 }
  48.                 if (strlen(word) < strlen(min))
  49.                 {
  50.                     strcpy(min, word);
  51.                 }
  52.             }
  53.             j = 0;
  54.         }
  55.     }
  56.     if (flag)
  57.     {
  58.         printf("The largest palindrome is '%s' and smallest palindrome is '%s' in '%s'.\n", max, min, string);
  59.     }
  60.     else
  61.     {
  62.         printf("No palindrome words exists in '%s'.\n", string);
  63.     }
  64.  
  65.     return 0;
  66. }
  67.  
  68. int palin_check(char str[])
  69. {
  70.     int i, len;
  71.  
  72.     len = strlen(str);
  73.     for (i = 0; i < len / 2; i++)
  74.     {
  75.         if (tolower(str[i]) != tolower(str[len - (i + 1)]))
  76.         {
  77.             return 0;
  78.         }
  79.     }
  80.  
  81.     return 1;
  82. }

$ gcc largesmallpalin.c 
$ ./a.out
Enter string: hello madam we speak malayalam
The largest palindrome is 'malayalam' and smallest palindrome is 'madam' in 'hello madam we speak malayalam'.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.