Hey I am trying to code a program that would take data from an input file and put the strings into an array and then output "True" if it is a Palidrome or "False" if it isn't. However I need help with coding the part that allows the program to read the string from a file and then put into an array. Also the expected output is supposed to be "Racecar: True" "Test: False" However I am getting, "Racecar: True "Racecar False "Test False "False:" I have also attached a picture of the output. I would gladly be grateful for any help I can get. This is my current code:
#include <stdio.h>
#include <stdlib.h>``
#include <ctype.h>
#define MAX 255
int palindrome(char *string, int x, int y)
{
if (x >= y)
return 0;
while (y > x)
if (tolower(string[x++]) != tolower(string[--y]))
return 0;
return 1;
}
int palindrome1(char *string, int x, int y)
{
if (x <= y)
return 0;
while (y < x)
if (toupper(string[x--]) != toupper(string[++y]))
return 0;
return 1;
}
int main()
{
char reading[MAX+1];
char readfile[MAX+1];
int x, y, i;
FILE *r;
puts("Enter read file name");
scanf("%s", readfile);
r=fopen(readfile, "rt");
if(r==NULL)
perror("File does not exist");
else {
while (fgets (reading, MAX, r) != NULL) {
x = 0;
do {
while (reading[x] && !isalpha (reading[x]))
x++;
y = x;
while (isalpha (reading[y]))
y++;
if (palindrome (reading, x, y)) {
printf ("Is a Palidrome= True: ");
for (i=x; i<y; i++)
printf ("%c", reading[i]);
printf ("\n");
}
else (palindrome1 (reading, x, y)) ;{
printf ("Not A Palidrome= False: ");
for (i=x; i<y; i++)
printf ("%c", reading[i]);
printf ("\n");
}
x = y;
}
while (reading[x]);
[enter image description here][1] }
fclose(r);
}
return 0;
}