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

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

This C program finds the largest and smallest possible words, which are palindromes, from a given input sentence.

Problem Statement

Write a C program that prompts the user to enter a sentence and finds the largest and smallest possible words that are palindromes within the sentence. The program should consider the case-insensitivity of palindromes, treating uppercase and lowercase letters as the same.

A palindrome is a word, phrase, number, or sequence of characters that reads the same forward and backward.

The program should perform the following steps:

  1. Prompt the user to enter a sentence.
  2. Read the input sentence.
  3. Process the input sentence and identify palindromic words.
  4. Consider each word in the sentence by iterating through the characters.
  5. Convert the word to lowercase for case-insensitive comparison.
  6. Determine if the word is a palindrome by comparing the characters from the beginning and end.
  7. Update the largestPalindrome and smallestPalindrome variables accordingly when a palindrome is found.
  8. After processing the entire sentence, output the largest and smallest palindromic words found.
  9. Handle words separated by spaces and consider them as separate entities.

Assumptions:

  • The maximum length of a word is 100 characters.
  • The maximum length of the input sentence is 100 characters.

Note: You can adjust these assumptions and constants in the program based on your requirements.

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

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_LENGTH 100

int isPalindrome(char *word) {
    int length = strlen(word);
    int i, j;

    // Convert word to lowercase for case-insensitive comparison
    for (i = 0; i < length; i++) {
        word[i] = tolower(word[i]);
    }

    // Check if word is a palindrome
    for (i = 0, j = length - 1; i < j; i++, j--) {
        if (word[i] != word[j]) {
            return 0;
        }
    }

    return 1;
}

void findPalindromes(char *sentence) {
    char word[MAX_LENGTH];
    char largestPalindrome[MAX_LENGTH] = "";
    char smallestPalindrome[MAX_LENGTH] = "";
    int length = strlen(sentence);
    int i, j, k = 0;

    for (i = 0; i <= length; i++) {
        if (sentence[i] != ' ' && sentence[i] != '\0') {
            word[k++] = sentence[i];
        }
        else {
            word[k] = '\0';

            if (k > 0 && isPalindrome(word)) {
                if (strlen(word) > strlen(largestPalindrome)) {
                    strcpy(largestPalindrome, word);
                }
                if (strlen(word) < strlen(smallestPalindrome) || strlen(smallestPalindrome) == 0) {
                    strcpy(smallestPalindrome, word);
                }
            }

            k = 0;
        }
    }

    printf("Largest palindrome: %s\n", largestPalindrome);
    printf("Smallest palindrome: %s\n", smallestPalindrome);
}

int main() {
    char sentence[MAX_LENGTH];

    printf("Enter a sentence: ");
    fgets(sentence, sizeof(sentence), stdin);

    // Remove newline character from the end of the input
    sentence[strcspn(sentence, "\n")] = '\0';

    findPalindromes(sentence);

    return 0;
}

How it Works

  1. The program starts by prompting the user to enter a sentence.
  2. The input sentence is read and stored in a character array.
  3. The program defines a helper function called isPalindrome to check if a given word is a palindrome. This function takes a character array (word) as input and returns 1 if the word is a palindrome, and 0 otherwise.
  4. The program defines another function called findPalindromes to process the input sentence and find the largest and smallest palindromic words. This function takes the input sentence as input.
  5. Within the findPalindromes function, a character array word is defined to temporarily store each word while processing the sentence.
  6. The variables largestPalindrome and smallestPalindrome are initialized as empty character arrays.
  7. The length of the sentence is calculated using the strlen function, and a loop is used to iterate through each character of the sentence.
  8. Inside the loop, each character is checked to determine if it is a space or the end of the sentence.
  9. If a character is not a space or the end of the sentence, it is appended to the word array.
  10. When a space or the end of the sentence is encountered, the word array is null-terminated, and the isPalindrome function is called to check if the word is a palindrome.
  11. If the word is a palindrome, it is compared with the existing largestPalindrome and smallestPalindrome. If the word is larger than the largestPalindrome or smaller than the smallestPalindrome, it is updated accordingly.
  12. After processing all the words in the sentence, the largestPalindrome and smallestPalindrome are printed as the output.
  13. Finally, the main function is executed. It prompts the user to enter a sentence, reads the input, and calls the findPalindromes to function to process the sentence and find the largest and smallest palindromic words.

The program uses the standard library functions strcpy, strlen, tolower, and string comparison functions to perform various string operations and comparisons.

By executing this program with different input sentences, you can explore different examples of palindromic words and observe the program’s ability to identify the largest and smallest ones.

Input / Output

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

In this example, the input sentence is “Radar is a level”. The program identifies the largest palindrome as “radar” and the smallest palindrome as “a”. Note that the program ignores the case of the letters when determining palindromes, so it treats “Radar” and “radar” as the same word.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...