title: Finding palindromes in c, variable(s) messes up
body: <p>I am assigned to do a program, which searches from input file palindrome and if it finds, it is written to output file. I have to use for both buffers with dynamic memory allocation, and arrangements with the text should be done in functions. Word is amount of symbols separated by space.
  Between words there may be more spaces, they should be ignored.</p>
  
  <p>Problem of this program for me is that the program doesn't work as "x" value gets messed up (and maybe "y").</p>
  
  <pre><code>while (i[(*x)] &amp;&amp; i[(*x)]==' ')
  </code></pre>
  
  <p>As i've noticed this line(^) changes "x" value not as it should</p>
  
  <pre><code>#include&lt;stdlib.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;ctype.h&gt;
  
  #define MAX 255
  
  int check_if_palindrome(char *i, int x, int y)
  {
      if (x &gt;= y)
          return 0;
      while (x &lt; y)
          if (tolower(i[x++]) != tolower(i[--y]))
              return 0;
      return 1;
  }
  void searching_for_word(char *i, int *x, int *y)
  {
      while (i[(*x)] &amp;&amp; i[(*x)]==' ')
          (*x)++;
      (*y) = (*x);
      while (i[(*y)]!=' ')
          (*y)++;
  }
  
  int main()
  {
      char *reading;
      char reading_file[MAX];
      char *writing;
      char writing_file[MAX];
      int x, y, i, j;
      FILE *read;
      FILE *write;
      puts("File name for input :");
      scanf("%s", reading_file);
      puts("File name for output :");
      scanf("%s", writing_file);
      read=fopen(reading_file, "r"); 
      if(read==NULL)
          perror("File does not exist");
      write=fopen(writing_file, "w");
      if(write==NULL)
          perror("File cannot be created");
      else{
          reading=malloc(MAX*sizeof(char));
          writing=malloc(MAX*sizeof(char));
          while(fgets(reading, MAX, read)!=NULL) 
          {
              x=0;
              do{
                  searching_for_word(reading, &amp;x, &amp;y);
                  if(check_if_palindrome(reading, x, y)==1){
                      j=0;
                      for(i=x; i&lt;y; i++){
                      writing[j]=reading[i];
                      j++;
                      }
                      writing[j]='\n';
                      printf("%d\n", j);
                      for(i=0;i&lt;=j;i++)
                      fputc(writing[i], write);
                  }
                  x=y;
              }
              while (reading[x]);
          }
      }
      fclose(read);
      fclose(write);
      return 0;
  }
  </code></pre>