120 likes | 256 Views
CISC 130: Today’s Class. Recap Splitting with ‘Extract’ Pulling the Pieces Together. A practical look at #11. We read data from a file containing 2 columns We split data from the 2 columns Each goes into a separate array: numbers and names Index values ‘match’ for the column
E N D
CISC 130: Today’s Class • Recap • Splitting with ‘Extract’ • Pulling the Pieces Together R. Smith - University of St Thomas - Minnesota
A practical look at #11 • We read data from a file containing 2 columns • We split data from the 2 columns • Each goes into a separate array: numbers and names • Index values ‘match’ for the column • If names[5] = “Joe” then numbers[5] = his phone number • We search for names in ‘names’ array • then we print out the corresponding name and number R. Smith - University of St Thomas - Minnesota
Let’s create a sample file • Go to the assignment, copy/paste the data R. Smith - University of St Thomas - Minnesota
How the assignment works • Call a function to read the number/name strings into 2 separate arrays • One keeps the name strings • One keeps the number strings • Name[i] is the person whose number is in number[i] • Do a loop till a blank line is entered • Read a line from input • Look it up in the ‘names’ array; retrieve the index • If it’s a valid index, print out the name and number • Create a file of numbers/names for next step R. Smith - University of St Thomas - Minnesota
Suggested Strategy • Phase 1 (just finished) • Write a function to read strings into an array • A sentinel loop, looks for ‘null’ line • Write a function to print strings from an array • Call them from main • Phase 2 • Write a ‘split’ function to split lines (“extract” function) • Phase 3 • Rewrite the ‘read’ function to call the ‘split’ function • Phase 4 • The search and printout R. Smith - University of St Thomas - Minnesota
We just did Phase 1 • How does Phase 2 work? • How do we split names from numbers? • we need some help… • Maybe we look up the Gnu C Library again? R. Smith - University of St Thomas - Minnesota
Standard string.h functions • strlen() – length of a string • strspn() – skips over a set of chars • Input1: string to check • Input2: string of chars to skip over • Output: offset to first char that’s not in Input2 • strncpy() – copies a string to a destination • Input1: destination • Input2: source • Input3: size of destination R. Smith - University of St Thomas - Minnesota
Writing the extract() function • Local variables • We need 2 array indices • We need a variable for the ‘split’ index • First, find the split point • The end of the number • Next, copy out the name and number • Option: copy the number • Option: copy the name R. Smith - University of St Thomas - Minnesota
An extract() function void extract (char str[], char name[], char number[]) { inti, j; // indices to copy name int split; // split point between name and number split = strspn(str, "01234567890-() "); strncpy(number, str, SIZE); number[split] = '\0'; j = 0; i = split; while (str[i] != '\0') { name[j] = str[i]; j = j + 1; i = i + 1; } name[j] = '\0'; } R. Smith - University of St Thomas - Minnesota
Searching • We can use a ‘full match’ from the library • We can write a ‘partial match’ R. Smith - University of St Thomas - Minnesota
What’s Left? • READ THE ASSIGNMENT and clean up any loose ends… • Write a function to do ‘partial match’ • Replace ‘strcmp’ with the partial match function • The program should print out the first name that the partial string matches • Put a loop in main() that looks up names until the user types a blank line. • Change LCNT to handle the full sized file • Get rid of unnecessary printouts
Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. R. Smith - University of St Thomas - Minnesota