-3

I'd like to input strings dynamically using C.

input: words which are consist of letters.

0 < the nubmer of words N < 1000

the max length of a word <= 30

input example) gh a b f cde

Besides, I will sort them in alphabetical order. May I use array or something?

I tried to use gets but entered error...

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
oops
  • 93
  • 1
  • 8
  • Please show your current attempts so that we may assist. – starlight Apr 17 '17 at 01:28
  • @starlight Thanks for the comment. To my shame, I don't know how to access this questiion exactly even I googled a lot. – oops Apr 17 '17 at 01:44
  • Try to break it into small parts. Start by looking at the string.h API to see if there are any functions that could possibly be helpful for accomplishing this task. Dynamic String input and sorting those strings are two separate questions in my opinion. Try taking a look here for information about reading a string as input with an unknown length: http://stackoverflow.com/questions/16870485/how-can-i-read-an-input-string-of-unknown-length – starlight Apr 17 '17 at 01:50
  • Yes, you can use an array, and probably dynamic allocation though for less than 1,000 words of up to 30 characters each (plus null bytes), you could use a fixed allocation of less than 32 KiB. You should never use `gets()` — it is [never safe to use `gets()`](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). – Jonathan Leffler Apr 17 '17 at 02:10
  • You should also show what you've tried — create an MCVE ([MCVE]). It isn't entirely clear whether you have one word per line, or whether you can have multiple words per line. Both can be handled; they just require different sorts of care. – Jonathan Leffler Apr 17 '17 at 02:20
  • Thanks guys. I've made it by using malloc, array, scanf. – oops Apr 18 '17 at 05:10

1 Answers1

0

You are guaranteed that the max length of a word is 30, meaning char word[31]; would suffice for each word:

#define WORDSIZE 31
#define WORDLEN_FMT "%30s" //width must be WORDSIZE-1!

/* Read a space-delimited word from `fileptr` and store it in `word`.
 *
 * Returns:
 *      0 - maybe more words to read
 *      1 - no more words to read
 *     -1 - error occurred while reading input
 */
int get_word(FILE *fileptr, char word[WORDSIZE]) {
    size_t i;

    switch (fscanf(fileptr, WORDLEN_FMT, word)) {
    // EOF or an error was encountered.
    case EOF:
        if (ferror(fileptr)) {
            return -1;
        }
        /* Fallthrough for `feof() != 0`. */
    // If nothing was stored in `word`, then we're done.
    case 0:
        return 1;
    }
    // A word was stored, so keep going!
    return 0;
}

As for reading all words, you could either allocate 1000 words initially, or you could deal with dynamic allocation and grow the array capacity by some factor x as necessary; I'd personally pre-allocate the 1000 words since I know there will never be more than 1000. Whichever you choose, it'll be important to keep track of the length of the array of words since it's unlikely that you'll use every bit of the memory you allocate (i.e. array_length < array_capacity usually.) I'll leave this up to you.

Finally, you can sort words easily with qsort and a wrapper around strcmp:

// Compare the words pointed to by `a` and `b`.
int compare_words(const void *a, const void *b) {
    return strcmp(*(const char **)a, *(const char **)b);
}

...

// Sort `word_array` using the `compare_words` function.
// The array contains `word_count` words, each with size `WORDSIZE`.
qsort(word_array, word_count, WORDSIZE, compare_words);
  • According to the Unabridged Dictionary, you are guaranteed the longest word is `28-chars` (*Antidisestablishmentarianism*) so `29` chars will do `:)` There is also the `28-char` (*Interdenominationalistically*). – David C. Rankin Apr 17 '17 at 05:59