In this workshop, you will work with the string library functions to build a formatted string out of raw data.

LEARNING OUTCOMES

Upon successful completion of this workshop, you will have demonstrated the abilities:

  • use C-stype string definition to resize and modify strings
  • use strcpy to copy strings
  • use strlen to find the length of strings
  • use strcmp to compare two strings
  • use strcat to concatenate one string to another

IN-LAB

There are two text data-files in this lab; starwars_directory.csv and jedi_master.txt. starwars_directory.csv holds the phone number of all the StarWars heroes in the galaxy. This file contains names and their phone numbers in the following format:

Full Name,phone number< NEWLINE>

Full Name ,NPA CO NUMBER< NEWLINE>
Maximilian Veers,416 555 0128< NEWLINE>

Full Name length is less than or equal to 30 characters in file.

NPA is exactly 3 characters.

CO is exactly 3 characters.

NUMBER is exactly 4 characters.

jedi_master.txt holds the names of all the Jedi Masters in the galaxy. This file contains one full name per line that is less than or equal to 30 characters.

Open 100_w10.c and complete the code of two functions to format the phone records in a string.

int isJediMaster(const char name[]);

Reads the full-names out of the Jedi Master file, one by one, and compares each of them with the incoming name argument, if there is a match it will return true (1) otherwise false (0);

Note: Remember that you must have only one return statement in a function.

This function will be used to check and see if the name read from phone directory belongs to a Jedi Master.

void formatJediPhoneRecord(char formattedRecord[],
const char fullName[],
const char npa[],
const char co[],
const char number[]);

Receives Jedi phone record through fullname, npa, co and number arguments and formats the phone record into one C-style string as follows:

Padme Amidala (418) 555-0105
Mace Windu (438) 555-0155 Jedi Master.
Emperor Palpat.. (450) 555-0143

Formatting steps:

First if the name is longer than 16 characters it will shorten it to 14 and then concatenates two dots ("..") to the end of it to make it exactly 16 characters. By doing this the viewer will notice if a name is shortened. (see third line in the example above)

Otherwise (if the name is shorter than 16) to make the name exactly 16 characters, it will concatenate a string full of space with the length of (16 Length of name). (see lines 1 and 2 in the example above)

This name will be copied into formattedRecord.

Then concatenate the following the formattedRecord.

1- a space and an open parentheses
2- npa string
3- Close parentheses and a space
4- co string
5- a dash ("-")
6- number string
7- if this the name is of a Jedi Master, concatenate (" Jedi Master") (see third line in the example above)

After completing the two function, the workshop should generate the following output:

Nien Nunb (403) 555-0163
Baron Notluwis.. (587) 555-0155
Bib Fortuna (780) 555-0179
Salacious B Cr.. (902) 555-0167
Jar Jar Binks (867) 555-0149
Biggs Darkligh.. (226) 555-0119
Wilhelm Scream.. (249) 555-0122
Maximilian Veers (289) 555-0128
Cornelius Evazan (343) 555-0180
Anakin Skywalker (365) 555-0110 Jedi Master
General Grievous (416) 555-0147
Darth Maul (437) 555-0160
Grand Moff Tar.. (519) 555-0131
Mon Mothma (613) 555-0196
Count Dooku (647) 555-0140
Lando Calrissian (705) 555-0132
Admiral Motti (807) 555-0102
Wedge Antilles (905) 555-0100
Padme Amidala (418) 555-0105
Mace Windu (438) 555-0155 Jedi Master
Emperor Palpat.. (450) 555-0143
Qui-Gon Jinn (514) 555-0138 Jedi Master
Jabba the Hutt (579) 555-0178
Admiral Ackbar (581) 555-0120
Chewbacca (819) 555-0168
Yoda (873) 555-0153 Jedi Master
Boba Fett (306) 555-0131
Luke Skywalker (639) 555-0176
R2-D2 (867) 555-0121
C-3PO (403) 555-0113
Han Solo (250) 555-0161
Princess Leia (604) 555-2121
Obi-Wan Kenobi (365) 555-3113 Jedi Master
Darth Vader (416) 555-4161

AT-HOME

Please provide answers to the following questions in a text file named reflect.txt.

WORKSHOP QUESTIONS:

1-Compare a string to an array of chars. What is the difference?

2-What format string do you use to ensure that the number of input characters read by scanf() into a C-style string does not exceed the 10 bytes of space that you have allocated for the string?

3-Why does strcmp(const char*, const char*) return 0 for two matching strings?

4-If you concatenate "Hello" to "C" how many bytes of memory do you need to store the result?

SUBJECT SURVAY:

5-What was the most interesting thing you learned this semester?

6-Do you feel that the quizzes about the weeks readings helped you learn more than you might have otherwise done.

7-Are there any things that you particularly like about the way that the course is delivered?

8-Are there any things that you particularly dislike about the way that the course is delivered?

9-Is there anything you would like to see added to the way the course is delivered?

10-How would you rate your level of understanding of the course topics?

a. Very good
b. Pretty good
c. Adequate
d. Poor

11-Did you enjoy doing the workshops? Why?

12-Did you enjoy the LabA activities?

13-Do you feel that LabA helped you understand how to think like a programmer?

14-The content of this course was

a. Too little
b. Just right
c. Too much
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.