title: C Programming Word Search not scanning through a second time
body: <p>I am working on a wordsearch project that will scan a 18 by 18 matrix of letters and find words hidden in any orientation.</p>
  
  <p>I succeeded in reading the matrix from a file and printing it onto the console. and the wordsearch will scan the first row of letters correctly.</p>
  
  <p>But if you ask it to find a word located 5 or 6 rows down, it won't do it.</p>
  
  <p>Also, if say you are looking for the word "roosevelt" and it starts on X=5,Y=4. If the word search finds the letter "r" on the first row, it will stop searching and say it only found the first letter for "roosevelt". </p>
  
  <p>I am not sure how to get the program to keep searching the whole matrix until the matrix finishes or the whole word is found.</p>
  
  <p>Here is my code:</p>
  
  <p>(I also have a dropbox link that has the source code ready and the word matrix file):
  <a href="https://www.dropbox.com/s/g5z3frtezjoj2vr/WordSearch.zip" rel="nofollow">https://www.dropbox.com/s/g5z3frtezjoj2vr/WordSearch.zip</a></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt; //Used for the'toupper' function
  
  
  
  int main(void){
  
   int N=0,M=0,a,X,Y,i,length;
      char array[18][18], string[50];
  
   FILE *wsch;
   wsch = fopen("wrdsearch.txt","r");
  
  
  /* Above defines the 18X18 array in which to fit the wordsearch
     as well as a search string of length 50 (more than enough to search for words within      the wordsearch) */
  printf("WORDSEARCH SOLVER - PRESS CTRL-C TO QUIT\n\n");
  
    if(!wsch) 
    {
      printf("Error! File did not open.\n");
      return 1;
    }
  
      for (Y=0; Y&lt;18 ;Y++)
      {
          for(X=0;X&lt;18;X++)
          {
              fscanf(wsch,"%c\n",&amp;array[Y][X]); //Inputting each character into array
              array[Y][X]=toupper(array[Y][X]);
          }
      }
       printf("   ");
     for (X=0;X&lt;18;X++)
      {
          if(X&lt;=9){
          printf("%d ", X);
  
          }
          else{
          printf("%d", X);
  
          }
      }
  
     printf("\n______________________________________\n");
     fclose(wsch);
      for (Y=0;Y&lt;18;Y++)
      {
          if(Y&lt;=9){
          printf("%d |", Y);
          }
          else{
          printf("%d|", Y);
          }
          for (X=0;X&lt;18;X++)
          { 
              printf("%c ",array[Y][X]); //Printing output to screen
          }
          printf("\n");
      }
      printf("\nPlease enter the word to be searched:\n");
      fgets(string,50,stdin); //Using the fgets function to read in the string from keyboard - prevents buffer overflow
      for(i=0;string[i]!='\0';i++)
      {
          string[i]=toupper(string[i]); //Converts string all into uppercase to prevent errors
      }
      length=strlen(string);
      for (Y=0;Y&lt;18;Y++)
      {
          for(X=0;X&lt;18;X++)
          {
  
              if(array[Y][X]==(string[0]))
              {
          printf("\nLetter 1 of %sis found at %d,%d\n",string,X,Y);
      //Success in finding first letter of string - Now trY in other directions
      if (array[--Y][X]==string[1])
      {
          //Success in finding in Y+ direction
          printf("Letter 2 of %sis found at %d,%d",string,X,Y);
          for (a=2;a&lt;length;a++)
          {
              if (array[--Y][X]==string[a])
                  printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
          }
          printf("\n\nWord found in Y+ direction");
          break;
      }
      else if (array[Y][++X]==string[1])
      {
          //Success in finding in Y+X+ direction
          printf("Letter 2 of %sis found at %d,%d",string,X,Y);
          for (a=2;a&lt;length;a++)
          {
              if (array[--Y][++X]==string[a])
                  printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
  
  
          }
          printf("\n\nWord found in Y+X+ direction");
          break;
      }
      else if (array[++Y][X]==string[1])
      {
          //Success in finding in X+ direction
          printf("Letter 2 of %sis found at %d,%d",string,X,Y);
          for (a=2;a&lt;length;a++)
          {
              if (array[Y][++X]==string[a])
                  printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
          }
          printf("\n\nWord found in X+ direction");
          break;
      }
      else if (array[++Y][X]==string[1])
      {
          //Success in finding in X+Y- direction
          printf("Letter 2 of %sis found at %d,%d",string,X,Y);
          for (a=2;a&lt;length;a++)
          {
              if (array[++Y][++X]==string[a])
                  printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
          }
          printf("\n\nWord found in X+Y- direction");
          break;
      }
      else if (array[Y][--X]==string[1])
      {
          //Success in finding in Y- direction
          printf("Letter 2 of %sis found at %d,%d",string,X,Y);
          for (a=2;a&lt;length;a++)
          {
              if (array[++Y][X]==string[a])
                  printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
          }
          printf("\n\nWord found in Y- direction");
      //    break;
      }
      else if (array[Y][--X]==string[1])
      {
          //Success in finding in X-Y- direction
          printf("Letter 2 of %sis found at %d,%d",string,X,Y);
          for (a=2;a&lt;length;a++)
          {
              if (array[++Y][--X]==string[a])
                  printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
          }
          printf("\n\nWord found in X-Y- direction");
     //     break;
      }
      else if (array[--Y][X]==string[1])
      {
          //Success in finding in X- direction
          printf("Letter 2 of %sis found at %d,%d",string,X,Y);
          for (a=2;a&lt;length;a++)
          {
              if (array[Y][--X]==string[a])
                  printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
          }
          printf("\n\nWord found in X- direction");
     //     break;
      }
      else if (array[--Y][X]==string[1])
      {
          //Success in finding in X-Y+ direction
          printf("Letter 2 of %sis found at %d,%d",string,X,Y);
          for (a=2;a&lt;length;a++)
          {
              if (array[--Y][--X]==string[a])
                  printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
          }
          printf("\n\nWord found in X-Y+ direction");
     //     break;
      }
    }
      else
      {
  
  
  
      }
  
          }
         printf("\n%d|%d\n", X,Y);
         break;
      }
  
  //End of file opening correctly process
  
  printf("\n");
  system("pause");
  return 0;
  }
  </code></pre>
  
  <p>Data file:</p>
  
  <pre><code>M N O S L I W E R E L Y T L E A G N
  A H O O V E R T A Y L O R V E N N A
  D F D R O O S E V E L T O N O M I M
  I N T P M H I E G D I L O O C O D U
  S O N L I J Q A D A M S S R N N R R
  O X L O G E F F M O I R E E G R A T
  N I B T S O R A O R M O V A W O H N
  F N H U R R B C R N L R E G B E W E
  R O S D C O E A E I O N L A U A J R
  K W U N L H H F N I O S T N S W A U
  E R B A J B A C F S S M N H H R D B
  N E W L O T O N K E C E I H T H A N
  N T H E H L A C A K J N N H O I M A
  E R G V N C A F I N G H U H A J S V
  D A R E S J C N T T A R N B O Y A E
  Y C A L O D L N O S I R R A H W E O
  N E N C N E T N N O T N I L C O E S
  D A T Y Y P O L K G A R F I E L D R
  </code></pre>