Code is in C programming and should work for visual studio 2015 Use #include &&
ID: 3738259 • Letter: C
Question
Code is in C programming and should work for visual studio 2015
Use #include && #include
Also use for loop & read the input.txt document to check
DO NOT USE ANYTHING LIKE "gets(filename)" or #include<stdlib.h> or #include<conio.h> PLEASE
use while (fscanf(inp, "%s", &text) != EOF) or something similar PLEASE
output file should print only the words that are palindromes.
input.txt file:
2 Palindromes 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 identifies palindrome words. Your program should: Read words to be checked from a file called input.txt (You can find this file on D2L). If a word is a palindrome it prints it to a file named palindromes.txtExplanation / Answer
#include <stdio.h>
int isPalindrome(char str[])
{
int len, flag = 0;
for(len=0; str[len]!=''; len++); //used to get the length of char array
for(int i=0, j=len-1; i<j; i++, j--) //used to move the first char to next position, last char to its prev position and so on..
{
if(str[i] != str[j])
{
flag = 1;
break;
}
}
if(flag == 0)
return 1;
else
return 0;
}
int main()
{
char text[100];
FILE *inp = fopen("input.txt", "r");
FILE *out = fopen("palindromes.txt", "w+");
if (inp == NULL)
{
printf("File not found ");
exit(0);
}
while (fscanf(inp, "%s", text) != EOF) {
if(isPalindrome(text))
fprintf(out, "%s ", text);
}
fclose(out);
}
Output : Palindromes.txt:
aibohphobia
alula
cammac
civic
deified
deleveled
detartrated
devoved
dewed
evitative
hannah
kayak
kinnikinnik
lemel
level
madam
malayalam
minim
murdrum
peeweep
racecar
radar
redder
refer
reifier
repaper
reviver
rotator
rotavator
rotor
sagas
solos
sexes
stats
tenet
terret
testset