Write a C program which carries out the following:Reads a file, and prints all palindrome words in the file. A word is considered to be separated only on whitespace. Each palindrome should be printed on a new line.
Question
Write a C program which carries out the following:Reads a file, and prints all palindrome words in the file. A word is considered to be separated only on whitespace. Each palindrome should be printed on a new line.
Solution
Here is a simple C program that reads a file and prints all palindrome words in it. This program assumes that the words in the file are separated by whitespace.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Function to check if a word is palindrome
int isPalindrome(char* word) {
int len = strlen(word);
for(int i = 0; i < len/2; i++) {
if(word[i] != word[len-i-1]) {
return 0;
}
}
return 1;
}
int main() {
FILE *file = fopen("file.txt", "r");
if (file == NULL) {
printf("Could not open file\n");
return 1;
}
char word[100];
while (fscanf(file, "%s", word) != EOF) {
if (isPalindrome(word)) {
printf("%s\n", word);
}
}
fclose(file);
return 0;
}
This program works as follows:
- It opens the file "file.txt" in read mode.
- It reads each word from the file using the
fscanffunction. - For each word, it checks if the word is a palindrome using the
isPalindromefunction. - If the word is a palindrome, it prints the word.
- Finally, it closes the file.
Please replace "file.txt" with the path to your file. Also, this program assumes that no word in the file is longer than 99 characters. If there are longer words, you should increase the size of the word array.
Similar Questions
Write a programme to check whether given input is palindrome or notConstraintsABC != PalindromeMAM == Palindrome123 != Palindrome151 == Palindrome
Write a Java program to print the smallest and biggest possible palindrome word in a given string. If no palindrome is found in the given string, then, print 'no palindrome'.Input FormatIn the first line read a stringOutput FormatThe first line contains the smallest palindrome word (or) no palindromeThe nextline contains the biggest palindrome word
Problem statementWrite a program such that it takes a lower limit and upper limit as inputs and prints all the intermediate palindrome numbers excluding the boundaries. ExampleInput 10 80 Output11 22 33 44 55 66 77
Write a python program to check the given string is palindrome or not.
Your friend is in university and there is a coding challenge going on. A string S of lowercase letters is displayed over the screen. The length of the string is N. An integer M is also displayed over the screen.Your task is write a program to determine if we can make the string Palindrome by performing at most M operations on the provided string
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.