0

I have a project for a c 99 programming class that requires us to ask a user for a sentence and then take that sentence char-by-char and store each char individually in a linked list. We were told that we need to use getc() and a while loop to read through the sentence and store into the linked list.

So far I know that you need the while loop to go until getc encounters a " \n". Other than that I do not even know where to begin. I have not had a problem with this class other than this project.

Any help on an approach or other ways to do this would be very much appreciated.

Kara
  • 6,115
  • 16
  • 50
  • 57

1 Answers1

1

The most important part of the assignment is not reading the input (there's not much more to it than the looped getc), but knowing what a linked list is, and how one would make such a linked list. Look this up. To get you started, a linked list looks like this:

A linked list

You'll need good knowledge of pointers, malloc, free and structs. Look up those subjects as well if you're still stuck.

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • Will malloc and free need to be in the while loop or are they only used initially? – user2316765 Apr 24 '13 at 18:12
  • You'll need to allocate more memory as new characters come in. You should free the list only after you're done using it (and don't forget that you should free the whole list, not just the first element). – Wander Nauta Apr 24 '13 at 18:17