
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print longest palindrome word in a sentence in C Program
Given a sentence and the challenge is to find the longest palindrome from the given sentence
What is a Palindrome?
Palindrome is a word or sequence whose meaning remains same even after reversing the string
Example − Nitin, after reversing the string its meaning remains the same.
Challenge is to find the longest palindrome from the given sentence.
Like sentence is: malayalam liemadameil iji
It contains three palindrome words but the longest is − liemadameil
Algorithm
START STEP 1 -> Declare start variables I, j, k, l, max to 0, index to -1, check to 0, count to 0 Step 2 -> Loop For i to 0 and i<strlen(str) and i++ Set max =0, k =i and j=i+1 Loop While str[j]!=' ' and str[j]!='\0' Increment j by 1 End While Set l=j-1 IF str[k]!=' ' and str[k]!='\0' Loop While k<=1 If str[k]==str[l] Increment max by 1 If count<=max Set index=i and count = max End If End IF Else Set max = 0, count = -1 Break End Else Increment k and I by 1 End Loop While End If Set i=j Step 3 -> End Loop For Step 4 -> Loop For i = index and i!=-1 && str[i]!=' ' && str[i]!='\0' and i++ Print str[i] Step 5 -> End Loop For STOP
Example
#include <stdio.h> #include <string.h> int main(int argc, char const *argv[]) { char str[] = {"malayalam liemadameil iji"}; int i, k, l, j, max =0, index = -1, check = 0, count = 0; for(i=0; i<strlen(str); i++) { max = 0; k = i; j = i+1; while(str[j]!=' ' && str[j]!='\0'){ j++; } l = j-1; if(str[k]!=' ' && str[k]!='\0') { while(k<=l) { if (str[k]==str[l]) { max++; if(count<=max) { index = i; count = max; } } else { max = 0; count = -1; break; } k++; l--; } } i = j; } for (i = index; i!=-1 && str[i]!=' ' && str[i]!='\0'; i++) { printf("%c", str[i]); } return 0; }
Output
If we run above program then it will generate following output.
liemadameil
- Related Articles
- C++ program for length of the longest word in a sentence
- Longest Palindrome in C++
- Count palindrome words in a sentence in C++
- Python program to sort Palindrome Words in a Sentence
- Java program to reverse each word in a sentence
- Python program to reverse each word in a sentence?
- Python program to find the smallest word in a sentence
- Python Program to replace a word with asterisks in a sentence
- Java Program to replace a word with asterisks in a sentence
- PHP program to replace a word with a different symbol in a sentence
- Java program to count the characters in each word in a given sentence
- Longest Word in Dictionary through Deleting in C++
- Print all palindrome permutations of a string in C++
- PHP program to find the first word of a sentence
- Sums of ASCII values of each word in a sentence in c programming

Advertisements