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.

1
  • So far I know that you need the while loop to go until getc encounters a " \n".... sounds like a good place to begin. You can also try making your linked list Commented Apr 24, 2013 at 18:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Will malloc and free need to be in the while loop or are they only used initially?
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).

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.