Lab - University of St. Thomas

Download Report

Transcript Lab - University of St. Thomas

CISC 130: Today’s Class
• Recap
• Splitting with ‘Extract’
• Pulling the Pieces Together
3/28/2016
R. Smith - University of St Thomas - Minnesota
1
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
3/28/2016
R. Smith - University of St Thomas - Minnesota
2
Let’s create a sample file
• Go to the assignment, copy/paste the data
3/28/2016
R. Smith - University of St Thomas - Minnesota
3
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
3/28/2016
R. Smith - University of St Thomas - Minnesota
4
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
3/28/2016
R. Smith - University of St Thomas - Minnesota
5
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?
3/28/2016
R. Smith - University of St Thomas - Minnesota
6
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
3/28/2016
R. Smith - University of St Thomas - Minnesota
7
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
3/28/2016
R. Smith - University of St Thomas - Minnesota
8
An extract() function
void extract (char str[], char name[], char number[]) {
int i, 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';
}
3/28/2016
R. Smith - University of St Thomas - Minnesota
9
Searching
• We can use a ‘full match’ from the library
• We can write a ‘partial match’
3/28/2016
R. Smith - University of St Thomas - Minnesota
10
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/bysa/3.0/us/ or send a letter to Creative
Commons, 171 Second Street, Suite 300, San
Francisco, California, 94105, USA.
3/28/2016
R. Smith - University of St Thomas - Minnesota
12