Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Print longest palindrome word in a sentence in C Program
In C programming, finding the longest palindrome word in a sentence involves parsing individual words and checking if they read the same forwards and backwards. A palindrome is a word that remains identical when its characters are reversed.
What is a Palindrome?
A palindrome is a word or sequence that reads the same backward as forward. For example, "malayalam" is a palindrome because reversing it gives "malayalam" − the same word.
Examples of palindromes: radar, level, madam, racecar
Syntax
int isPalindrome(char word[], int start, int end); void findLongestPalindrome(char sentence[]);
Algorithm
The algorithm works by extracting each word from the sentence and checking if it's a palindrome using two pointers −
1. Initialize variables for tracking longest palindrome 2. For each character in the sentence: - Extract the current word - Check if the word is a palindrome using two pointers - If palindrome length > current max, update the longest 3. Print the longest palindrome found
Example
Here's a complete program to find the longest palindrome word in a sentence −
#include <stdio.h>
#include <string.h>
int isPalindrome(char word[], int start, int end) {
while (start < end) {
if (word[start] != word[end]) {
return 0; // Not a palindrome
}
start++;
end--;
}
return 1; // Is a palindrome
}
int main() {
char str[] = "malayalam liemadameil iji radar";
int i, k, l, j, maxLen = 0, index = -1, wordLen = 0;
for (i = 0; i < strlen(str); i++) {
// Skip spaces
if (str[i] == ' ') continue;
k = i; // Start of word
j = i;
// Find end of current word
while (str[j] != ' ' && str[j] != '\0') {
j++;
}
l = j - 1; // End of word
wordLen = l - k + 1;
// Check if current word is palindrome
if (isPalindrome(str, k, l) && wordLen > maxLen) {
maxLen = wordLen;
index = k;
}
i = j - 1; // Move to end of current word
}
printf("Longest palindrome word: ");
if (index != -1) {
for (i = index; str[i] != ' ' && str[i] != '\0'; i++) {
printf("%c", str[i]);
}
} else {
printf("No palindrome found");
}
printf("<br>");
return 0;
}
Longest palindrome word: liemadameil
How It Works
The program uses these key steps:
- Word Extraction: Identifies word boundaries by finding spaces or null terminators
- Palindrome Check: Uses two pointers moving inward to compare characters
- Length Tracking: Keeps track of the longest palindrome found so far
- Index Storage: Remembers the starting position of the longest palindrome
Key Points
- The algorithm has O(n*m) time complexity where n is sentence length and m is average word length
- Space complexity is O(1) as it uses only a few variables
- Handles multiple palindromes by keeping track of the longest one
Conclusion
This approach efficiently finds the longest palindrome word by checking each word individually using the two-pointer technique. The program handles edge cases and provides a clear solution for palindrome detection in sentences.
