title: C counting word program works if there is no space between symbols, why?
body: <p>This is the code I'm using and it works perfectly unless:</p>
  
  <ol>
  <li>The text file starts with integers.</li>
  <li>If there is no space between symbols e.g [hi!] works perfectly  but [hi !] prints �</li>
  <li>If i put multiple symbols together e.g [?????????] the number of words will be incorrect.</li>
  </ol>
  
  <p>Just to clarify it is fine if there are integers within the code but not if they are at the start.</p>
  
  <p>Why is that the case? I am really curious how to solve that issue.</p>
  
  <p>EDIT:
  It was pointed out to me that fscanf does not support regex, why does it filter out the letters correctly then?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  /* counts words in the file */
  int main(void) 
  {
      FILE *fp;
      int r,n,i; /* a variable for result of a function, returning int */
       /* the words counter */
      const char *filename = "test2.txt"; /* a file name opening for read */
      char word[10]; /* an array for the check if a non-empty word was read */
  
      if ((fp = fopen(filename, "r")) == NULL) {
          fprintf(stderr, "error: file" "\n");
          return 1;
      }  
      /* if can't open the file for read
         then print an error message and return false to the environment */
      int arraylen = sizeof(word)/sizeof(word[0]); /*write the length of array word to arraylen*/ 
      n = 0; /* turn the counter of words to zero */
      word[0] = '\0'; /* turn the word array to an empty state */
     while ((r = fscanf(fp, "\n%10[^A-Za-z]%*c", word)) == 1) {
          printf("firstoutput\n");
          for(i=0;i&lt;arraylen;i++)
  
                  printf("%c",word[i]);
  
  
      if((r = fscanf(fp, "\n%[A-Za-z]%*c", word)) == 0) { /*in case next character is not a 
          letter do nothing, this is in place to prevent the program from getting stuck*/
  
           printf("secondoutput\n");
          for(i=0;i&lt;arraylen;i++)
              if(word[i] != (' '))
                  printf("%c",word[i]);
          }
  
          if (word[0] != '\0')
              n++;
          /* if the word array got something,
             then it was a word, count it */ 
  
          word[0] = '\0'; /* turn the word back into an empty state */
      }  
      /* skipping words delimeted by ' ' or '\n' or ','
         while file fp can be read, continue skipping
         and count every skip */
  
  
      if (ferror(fp) != 0) { /* check the file for read error if EOF occured */
          fprintf(stderr, "error: read file" "\n");
          fclose(fp);
          return 1;
      }
      /* if there was an error while reading the file
         then print error, close the file (because it was opened though)
         and return false to the environment */
  
      if (n == 1) /* control "to be" and endings for word or words */
          printf("\nthere is %d word" "\n", n);
      else
          printf("\nthere are %d words" "\n", n);
  
      fclose(fp); /* close the file */
  
      return 0; /* return success to the environment */
  }
  </code></pre>
  
  <p>EDIT2 : Im posting a full solution which works 101%, I combined all the various tips and stuff I found and created this, I believe this solution is extremely compact and efficient, if I am in the wrong do correct me !</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #define WORD "A-Za-z"
  
  
  int countWords(FILE *f,int* now){
     char ch[100][100];
     int x;
     *now=0;
     int count = 0;
         while (fscanf(f, "%[^" WORD "]",ch[count]) != EOF
          &amp;&amp; fscanf(f, "%10[" WORD "]",ch[count]) != EOF) {
  
          if(count&gt;0) {
  
          for(x=0;x&lt;count;x++) {
  
            if((strcasecmp(ch[count],ch[x]))==0) { /* Compare if the two words match, 
            case insensitive*/
              if(fscanf(f, "%[^" WORD "]",ch[count]) != EOF
              &amp;&amp; fscanf(f, "%10[" WORD "]",ch[count]) != EOF) /* Since the words match,
              now we have to overwrite the double word by scanning the next set of characters.*/
                printf("String is equal\n");
  
                (*now)++;
            }
          }
        }
          puts(ch[count]);
          count++;
          (*now)++;
      }
      puts(ch[13]);
      printf("%d\n",*now );
  
     return count;
  }
  
  int main(void){
  
     int uniquewordCount=0,wordCount=0;
     FILE *rFile = fopen("test2.txt", "r");
     uniquewordCount += countWords(rFile,&amp;wordCount);
     printf("%d\n",(a+b));
     printf("Amount of unique words: %d\n", uniquewordCount);
     printf("Amount of words: %d\n", wordCount);
     return 0;
  }
  </code></pre>