Complementary task for topic: 7
M Nemeth 2023-08-29 15:21:04.622218'
Strings: Palindromic Words in a Sentence
Strings: Palindromic Words in a Sentence
Create a C program that reads a sentence from the user and identifies and counts the palindromic words in the sentence. A palindromic word is a word that reads the same backward as forward.
Library functions can be used!
Hint: Use character arrays (char arrays) to store the sentence and individual words.
Prompt the user to enter a sentence.
Extract individual words from the sentence and check if they are palindromic Count the number of palindromic words in the sentence.
Display the palindromic words and their count as the output.
Solution
#include
#include
#include
// Function to check if a word is palindromic
int isPalindromic(char word[]) {
int length = strlen(word);
int i, j;
// Convert the word to lowercase for case-insensitive comparison
for (i = 0; i < length; i++) {
word[i] = tolower(word[i]);
}
// Check if the word is palindromic
for (i = 0, j = length - 1; i < j; i++, j--) {
if (word[i] != word[j]) {
return 0; // Not palindromic
}
}
return 1; // Palindromic
}
int main() {
char sentence[200];
char word[50];
int wordCount = 0;
int palindromicCount = 0;
printf("Enter a sentence: ");
scanf("%199[^\n]", sentence);
printf("Palindromic words: ");
for (int i = 0; sentence[i] != '\0'; i++) {
if (isalpha(sentence[i])) {
word[wordCount++] = sentence[i];
} else {
word[wordCount] = '\0'; // Null-terminate the word
if (wordCount > 0 && isPalindromic(word)) {
if (palindromicCount > 0) {
printf(", ");
}
printf("%s", word);
palindromicCount++;
}
wordCount = 0; // Reset wordCount for the next word
}
}
printf("\nNumber of palindromic words: %d\n", palindromicCount);
return 0;
}
Explanation
The program defines a character array (char array) sentence to store the input sentence, a character array word to store individual words, and integer variables wordCount and palindromicCount to keep track of word length and the count of palindromic words, respectively. The user is prompted to enter a sentence using printf() and scanf(). The %199[^\n] format specifier is used to read the entire line until a newline character (\n) is encountered, allowing the program to read a sentence with spaces. The program defines a function isPalindromic() to check if a given word is palindromic. The function takes a character array word as input and returns 1 if the word is palindromic and 0 otherwise. The function isPalindromic() converts the word to lowercase for case-insensitive comparison using tolower() function from the ctype.h library. The program uses a loop to traverse through each character of the sentence. Inside the loop, the program checks if the current character is alphabetic using isalpha() function from the ctype.h library. If it is alphabetic, the character is added to the word array. If a non-alphabetic character is encountered (such as a space or punctuation), the word array is null-terminated to form a complete word. The program then calls the isPalindromic() function to check if the word is palindromic. If the word is palindromic, it is printed on the console. The program keeps track of the number of palindromic words using the palindromicCount variable.