How to find out if word is read the same from beginning and end?

546 views Asked by At

so I'm writing a C program that needs to find words (in a text file) that are read the same from beginning AND end. So far, I've written a code that finds out if word's first and last letters are the same but can't really figure out what to do next. Any help, please? :)

  #include <stdio.h>
  #include <string.h>

const int CMAX  = 1000;
const int Dydis = 200;

void algorythm(char *line);

int main(){

void algorythm(char *line){                                        
char word[256];
char rezMasyv[256];
int i=0;
int j=0;
int k=0;
int z=0;

for (i=0;i<strlen(line);i++){                                 
    while (line[i]==' '){
        line[j]=' ';                                              
        ++j;
        ++i;
        }

        word[z]=line[i];
    ++z;
    if (line[i+1]==' ' || line[i+1]=='\n' || i+1==strlen(line)){
       if (word[0]!=word[z-1]){                                       
           for (k=0;k<z;++k){
               line[j]=word[k];
               ++j;
           }
       }
       z=0;
    }
}
line[j]='\0';
j=0;
}
        char duom[CMAX], rez[CMAX], text[Dydis];
FILE *duomFailas;
FILE *rezFailas;

printf("Enter the name of text file \n");
scanf("%s", duom);
duomFailas=fopen(duom, "r");
if (duomFailas==NULL){
    printf ("Error opening text file \n");
    system("pause");
    return 0;
};

printf("Enter the name of result file\n");
scanf ("%s", rez);
rezFailas=fopen(rez, "w");
if (rezFailas==NULL){
    printf ("Error opening results file \n");
    system("pause");
    return 0;
};

while (fgets(text, sizeof(text), duomFailas)) {                  
    algorythm(text);                                           
    fprintf(rezFailas,"%s\n",text);
}

    fclose(duomFailas);
    fclose(rezFailas);
    return(0);
}

The text file includes a lot of words seperated by a space ' '. I need to delete the words that are read from beginning and end the same (lol, samas, wololow and etc.).

Thanks for help ;)

2

There are 2 answers

0
Frerich Raabe On

You could have two pointers pointing to a word, one pointing to the first letter and another pointing to the last. You then have the two pointers walk towards each other, making sure that they both always point to the same character.

Something like:

#include <assert.h>
#include <string.h>

int is_palindrome(const char *s)
{
  const char *t;

  assert(s);

  for (t = s + strlen(s) - 1; t > s; ++s, --t) {
    if (*s != *t) {
      return 0;
    }
  }

  return 1;
}
0
Built In Parris On

There are definitely simpler ways to do what you're trying to do, unless you need to do it the way you've started above.

One example:

#include <stdio.h>
#include <string.h>

int main()
{
   char a[100], b[100];

   printf("Enter the string to check if it is a palindrome\n");
   gets(a);

   strcpy(b,a);
   strrev(b);

   if( strcmp(a,b) == 0 )
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a palindrome.\n");

   return 0;
}

Here's a website that has pre-existing palindrome(words the same forward and backward) programs written in C for your reference: http://www.programmingsimplified.com/c-program-find-palindrome