Skip to content
Physics and Astronomy
Home Our Teaching Resources C programming PHY3134 ex3.html
Back to top

Computational Physics
Week 3: Course work

You will probably want to use the code examples in the notes as the basis for your work, these can be cut and pasted from the Web pages at http://newton.ex.ac.uk/teaching/jmr/.

Also, life will be easier if you assume that names have no spaces so you can use %s and a sensible maximum name length.

  1. Take the skeleton address-book program from last week's notes and do enough to make it compile but not do anything, (ie #define some constants in a header file and write some dummy functions that do nothing but print "You've called this function". You will need to put the function prototypes in the header file).

  2. Now write the get_user_command function, making sure it only returns legal values.

  3. Decide on a suitable structure for a phone-book entry and define it, along with a suitable-named constant for the maximum number of entries, either at the top of your C file or in a header file. (Brief class discussion: How are you going to store the phone-book entries and ensure that every function that needs them has them? Pros and cons of different methods.)

  4. Now add the add_new_phonebook_entry function.

  5. Now add a simplified version of lookup_phonebook_entry which just goes through the whole list printing out every entry.

  6. The function strstr(haystack, needle) (where both haystack and needle are strings) returns non-zero when the first string contains the second, eg strstr("Football", "ball") is true but strstr("Football", "Leeds_Utd") is false.

    Modify your lookup_phonebook_entry function to read in a search string and just print out the entries that match. (You will need to #include <string.h> to use strstr).

      Is strstr case-sensitive?
    • What happens if the search string is blank (ie you just hit <return>)?
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Privacy & cookies Share
Back to top