Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First off, there is a big logic error. The two for loops:</p> <pre><code> for(i = 0; i &lt; 27; i++) { for(k = 0; k &lt; length; k++) { if(secretword[k] == alphabet[i]) { secretword[k] = '*'; } } } </code></pre> <p>Says:</p> <ul> <li>for all characters in the alphabet, <ul> <li>iterate over all characters in the string, and then <ul> <li>if that character in the string is equal to the current alphabet character: <ul> <li>replace it with an asterisk. </li> </ul></li> </ul></li> </ul></li> </ul> <p>Because you're iterating over the whole alphabet, you'll replace all of the string with '*'s. What you probably want is something like:</p> <pre><code>for(k = 0; k &lt; length; k++) { if(secretword[k] == guess) { secretword[k] = '*'; } } </code></pre> <p>instead.</p> <p>There are some other problems. This needs to be <em>after</em> secretword is read in:</p> <pre><code> length = strlen(secretword); </code></pre> <p>Otherwise you'll read the length of the uninitalised word. Change it to something like this:</p> <pre><code> printf("You Get six chances to guess all of the letters in a phrase\n"); printf("Enter the secret word/phrase: "); scanf("%s", &amp;secretword); length = strlen(secretword); </code></pre> <p>Also, this:</p> <pre><code> scanf("%s", &amp;guess); </code></pre> <p>Should probably be: </p> <pre><code> scanf("%c", &amp;guess); </code></pre> <p>since you're planning to only read a <code>char</code> rather than a string.</p> <hr> <p>Also, the 27 in this line:</p> <pre><code>char alphabet[27] = {"abcdefghijklmnopqrstuvwxyz"}; </code></pre> <p>Is correct, because you want to include the null terminator at the end of the string.</p> <p>However, this:</p> <pre><code>for(i = 0; i &lt; 27; i++) </code></pre> <p>Will read up to <code>alphabet[26]</code>, which will be a <code>'\0'</code>. You probably don't want to replace these <code>'\0'</code>s in the string (and you won't see any if you're only going up to <code>strlen(secretword)</code> characters - since <code>strlen()</code> counts up to the first <code>'\0'</code>). Changing the loop to only go to 26 characters stops you doing an unnecessary pass over the <code>secretword</code>. It should probably be </p> <pre><code>for(i = 0; i &lt; strlen(alphabet); i++) </code></pre> <p>Or, even better (as suggested by wildplasser):</p> <pre><code>char alphabet[] = {"abcdefghijklmnopqrstuvwxyz"}; .... for(i = 0; i &lt; sizeof alphabet -1; i++) </code></pre> <hr> <p>One last thing - your program will crash if you don't have enough space in the <code>secretword</code> array to hold the string read in. You can get around this by asking scanf to read only 19 characters:</p> <pre><code>scanf("%19s", &amp;secretword); </code></pre> <p>Note that scanf will terminate the string with a <code>'\0'</code>, so <code>%19s</code> may put up to 20 bytes in to the string.</p>
    singulars
    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.
    3. 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