Search code examples
cpalindrome

Finding words in a file, checking if they are palindrome


I've tried to write a program which finds a word(s) from reading file and checks if it is palindrome (same word from both sides) and if they are, they are saved into another file separated by return. Words in a reading file can be written in any way: separated by space, in a sentence or by return.

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define MAX 255

int palindrome(char *x, int y, int i) 
{
    while(i<=y){
        if(x[i]!=x[y])
            return 0;
        i++;y--;
    }
    return 1;
}

int main()
{
    char *reading; 
    int length;
    int x=0;
    int y=0;
    char read[MAX];
    char write[MAX];
    FILE *r;
    FILE *w;
    puts("Enter read file name");
    scanf("%s", read);
    puts("Enter write file name");
    scanf("%s", write);
    r=fopen(read, "r"); 
    if(r==NULL)
        perror("File does not exist");
    w=fopen(write, "w");
    reading=malloc(MAX*sizeof(char));
    while(fgets(reading, MAX, r)!=NULL) 
    {
        length=strlen(reading);
        while(x<=length){
            for(x=y; ;x++){
                printf("%c\n", reading[x]);
                if((reading[x]>='a'&& reading[x]<='z') || (reading[x]>='A' && reading[x]<='Z'))
                    break;
            }
            for(y=x; ;y++){
                printf("%c\n",reading[y]);
                if((reading[y]>='a'&& reading[y]<='z') || (reading[y]>='A' && reading[y]<='Z'));
                else
                    break;
            }
            if(palindrome(reading, y, x)==1)
                for( ;x<=y;x++)
                fputc(reading[x], w);
            x=y;
        }
    }
    fclose(r);
    fclose(w);
    return 0;
}

The problem is that code doesnt work, how to fix it?


Solution

  • I spotted an error in your code here with the wrong limit for x

    length=strlen(reading);
    while(x<=length){
    

    so I made quite a few changes to the way the probem is tackled rather than figuring out what other limits you might be breaking. I have written output to stdout rather than to a file. Note that I also added "t" to the file opening mode.

    #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 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 ("Palindrome: ");
                        for (i=x; i<y; i++)
                            printf ("%c", reading[i]);
                        printf ("\n");
                    }
                    x = y;
                }
                while (reading[x]);
            }
            fclose(r);
        }
        return 0;
    }