0
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main () 
{
 char word[100][21] ;

puts( "Enter Your Words" );

puts( "Enter STOP To Get Your Results" );

   while( strcmp( word, "STOP" ) ) 

   {
    scanf( "%20s", word );

   }

return 0;
}

After I scan in a word id like to store it into an array called storing[][], but I dont know how to achieve that, and also I dont want to store the terminating STOP word

5
  • Multiple problems in your code. First you're using word before you've given it any value in the while loop. Try using do {} while loop instead. Also word is a multidimensional array. You could do scanf("%20s", word[0]) for instance but using word alone isn't correct. Commented Mar 8, 2016 at 1:23
  • for ease of readability and understanding: consistently indent the code. Suggest 4 spaces per indent level (never use tabs for indenting). Indent after every opening brace '{'. Unindent before every closing brace '};. Commented Mar 8, 2016 at 19:24
  • posted code fails to index through the array word[]. posted code tries to compare contents of word with some string that has not yet been input. Posted code fails to check returned value (not parameter value) from scanf() to assure the operation was successful Commented Mar 8, 2016 at 19:28
  • Posted code contains 'magic' numbers (100, 21). 'magic' numbers make the code much more difficult to understand, debug, maintain. Suggest give the 'magic' numbers meaningful names via #define statements and then use those meaningful names throughout the code. Commented Mar 8, 2016 at 19:30
  • suggest: scaning into a local char array, then checking that array for the "stop" string. If equal to the "stop" string, then exit the input loop, otherwise use strcpy to copy the local char array to the word[x[ buffer. Then increment x in preparation for the next loop. You might want to make use of the for() statement for handling the variable x. Commented Mar 8, 2016 at 19:32

1 Answer 1

1

Code what you want.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#define WORD_MAX 100

int main (void)
{
    char storing[WORD_MAX][21]; /* you want the words stored into storing[][], not word */
    char buffer[21]; /* a buffer to store the word temporaly for not to store STOP to storing */
    int wordCount = 0; /* count how many words are stored */

    puts( "Enter Your Words" );

    puts( "Enter STOP To Get Your Results" );

    /* loop while there is room to store new word left in the array,
     * successfully read something and what is read is not the STOP word */
    while(wordCount < WORD_MAX && scanf("%20s", buffer) == 1 && strcmp(buffer, "STOP") != 0)
    {
        /* store the word read and increment the count */
        strcpy(storing[wordCount++], buffer);
    }

    /* sample code for testing: print what is read */
    {
        int i;
        for (i = 0; i < wordCount; i++) printf("%03d : %s\n", i, storing[i]);
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

What's the point in adding the braces around int i; and the for loop? Seems like clutter to me.
To create a block to define a variable inside so that the declared variable won't affect another part of the program.
Suggest #defined WORD_N 21 or the like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.