C programming A palindrome is a word that reads the same forward as it does back
ID: 3761128 • Letter: C
Question
C programming
A palindrome is a word that reads the same forward as it does backward. An example of a valid palindrome is the word “racecar”. An example of a word that is not a palindrome is “trust” (it reads backwards as “tsurt”). Develop a C program that identies palindrome words. Your program should:
• Read words to be checked from a le called input.txt (You can nd this le on the course website).
• If a word is a palindrome it prints it to a le named palindromes.txt
I can use strings and arrays,if comments could be added to de code, would be great so i can follow and understand it.
I hope someone can help me, thank you.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int isPalindrome(char arr[]) {
int len = strlen(arr);
int i;
for (i=0; i<len; i++) {
if (arr[i] != arr[len-1-i]) {
return 0;
}
}
return 1;
}
int main() {
FILE *in,*out;
char line[255];
in = fopen("input.txt", "r");
out = fopen("palindromes.txt", "w");
while (fgets(line,255, in)!=NULL) {
if (isPalindrome(line) == 1) {
fprintf(out, line);
}
}
fclose(in);
fclose(out);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.