Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are a few pointers besides the most important one of initializing <code>found[]</code>, which was mentioned in other comments.</p> <p>the <code>alpha[]</code> array complicates things, and you don't need it. See below for a modified file-read-loop that doesn't need the <code>alpha[]</code> array to count the letters in the file.</p> <p>And strictly speaking, the expression you're using to initialize the <code>alpha[]</code> array:</p> <pre><code>alpha[i] = 'A' + i++; </code></pre> <p>has undefined behavior because you modify <code>i</code> as well as use it as an index in two different parts of the expression. The good news is that since you don't need <code>alpha[]</code> you can get rid of its initialization entirely.</p> <p>The way you're checking for <code>EOF</code> is incorrect - it'll result in you acting on the last character in the file twice (since the <code>fscanf()</code> call that results in an <code>EOF</code> will not change the value of <code>ch</code>). <code>feof()</code> won't return true until <em>after</em> the read that occurs at the end of the file. Change your <code>ch</code> variable to an <code>int</code> type, and modify the loop that reads the file to something like:</p> <pre><code>// assumes that `ch` is declared as `int` while ( (ch = fgetc(fin)) != EOF ) { if ( isalpha(ch) ) { count += 1; ch = toupper(ch); // the following line is technically non-portable, // but works for ASCII targets. // I assume this will work for you because the way you // initialized the `alpha[]` array assumed that `A`..`Z` // were consecutive. int index = ch - 'A'; found[index] += 1; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload