-1

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;
}
  • 2
    Welcome to Stack Overflow. What is your actual question? Is something wrong with your current code? Does it fail to compile? Does it crash? Does it give answers that are wrong? Does it take too much time? – Beta Jun 10 '20 at 01:23
  • What's that??? `else (palindrome1 (reading, x, y)) ;{` – Dmitry Kuzminov Jun 10 '20 at 01:25
  • 1
    An offtopic question: who and why teaches students to use `scanf/printf`, `char[]`, etc. saying that that is C++?.. – Dmitry Kuzminov Jun 10 '20 at 01:27
  • I got that from a youtube tutorial. – The Gaming God Jun 10 '20 at 01:42
  • @Beta There is a bug with the output, also I would like to know how to implement the array part with the files. – The Gaming God Jun 10 '20 at 01:44
  • @TheGamingGod: Please edit your question to specify the expected output and the actual output. – Andreas Wenzel Jun 10 '20 at 01:45
  • 1
    You may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) Also this may be worth reading: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Jun 10 '20 at 01:47
  • Ohh I see, Thanks for the tip! – The Gaming God Jun 10 '20 at 01:58

1 Answers1

1

First of all, try to reformat your code to make it more readable (even for yourself). As a result this code:

                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;

may become:

                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;

The else clause looks suspicious as well as the next block.

I guess that the idea was like that:

                if (palindrome (reading, x, y)) {
                    printf ("Is a Palidrome= True: ");
                    for (i=x; i<y; i++)
                        printf ("%c", reading[i]);
                    printf ("\n");
                }
                else {
                    printf ("Not A Palidrome= False: ");
                    for (i=x; i<y; i++)
                        printf ("%c", reading[i]);
                    printf ("\n");
                }
                x = y;
Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40