Download presentation
Presentation is loading. Please wait.
Published byMaximillian Ferguson Modified over 6 years ago
1
ENEE150 Discussion 06 Section 0101 Adam Wang
2
Overview Strings Pointer Arrays Project 2
3
Strings No such thing as a String in C; represented as char arrays What’s the difference between a char array and a string? Strings end in the null terminating character “\0” Only strings can be used for the string library functions #include <string.h> Printf(“%s”, s); E N E E 1 5 \0 “ENEE150” =
4
int strlen (char *s) Takes in a string and returns the size (NOT including the “\0”) Uses the pointer and moves through the array until is sees the “\0” character strlen(“enee150”) -> 7 strlen(“a b c d e”) -> 9
5
char *strcpy (char *s1, char *s2)
Copies s2 into s1 and returns pointer to s1 How is this done? Function moves through both pointers putting each char from s2 into s1; stops when reaches “\0” strcpy(“enee150”, “math”) -> “math150” What if s2 bigger than s1? Only stops when it reaches the end of s2; this might cause an error What if s2 doesn’t have the “\0” It’ll just keep copying forever; this will definitely cause an error Why can’t we just do s2 = s1? Try writing this function and look up online how it’s actually written
6
int strcmp (char *s1, char *s2)
Returns whether s1 comes before or after s2 (alphabetically) Positive: s1 comes after s2 Negative: s1 comes before s2 Zero: s1 equals s2 Why can’t we just use if (s1 == s2) ? Strcmp(“bat”, “cat”) -> negative Strcmp (“cat”, “cat”) -> 0 Strcmp(“carpet”, car”) -> positive
7
char *strstr (char *s1, char *s2)
Looks for first occurrence of s2 in s1 and returns a pointer to it Returns null if not found strstr(“concatenation”, “cat”) -> “catenation” What do the following lines of code do? char *s3 = strstr(s1, s2); if (s3 != null) s3 = strstr(s3 + 1, s2);
8
Pointer Arrays Array of pointers Int a, b, c;
Int *parr[3] = {&a, &b, &c}; Char a[] = “I”; Char b[] = “Love”; Char c[] = “Enee150”; Char *arr[3] = {a, b, c}; 0x000.. I \0 0x008.. L o v e \0 0x00C… E n e e 1 5 \0
9
Why not use a 2D array? More wasted memory Bulkier
Char arr2[3][8] = {“I”, “love”, “enee150”}; More reasons later in the course I \0 L O V E N 1 5
10
Project 2 Due Thursday, Oct. 19 First part is due Oct. 12
Recipe Database: Read in recipes (strings), and parse them using the string functions Three data files: recipes0.data, recipes1.data, recipes2.data You will ask user which one to read in, then parse it and open the main menu
11
Arrays you should use for this project:
MAX_DATABASE_SIZE is 5MB, so this is 5*1024*1024 characters Raw_recipes: read entire file into this Title: read all the recipe titles in Author: authors of all the recipes Prep_time_hour: hour of each recipe Prep_time_min: minutes of each recipe Categories: Checklist for each category Ingredients_begin/end: stores the index where the ingredients begin/end Instructions_begin / end: store the indexes where the recipe instructions begin/end
12
More global vars All these are optional; depends how you implement your code all_categories will store each new category as you read it in num_categories will keep track of how many you’ve read in
13
The ****** isn’t part of it they just signify another recipe
For every recipe: Put the recipe name into the recipes array Author into author array Prep time hour/min into hour/min array For categories: Go through existing categories If current category matches, store the index into categories array If category doesn’t exist, add it to the all_categories array Error if more than 8 categories seen Recipe Header
14
Ingredients Nothing to parse here; just store the beginning/end index of the ingredients Alternatively, store the ingredients in a new 2d array
15
Instructions Again, store the beginning/end index of the instructions in the file array Or use a new array for instructions End index of Ingredients/ beginning index of instructions is the “Instructions:” line End index of instructions is the line containing the ********* Need to print # of recipes seen (basically whenever you see the****)
16
Parsing Method 1: use getc(inFile) to read in all the chars into the array Stop when getc(inFile) == EOF “end of file” Go to next line: i++ while i != ‘\n’ Skip spaces: i++ while i== ‘ ’ Method 2: use fgets(line, MAX_STRING_LEN, inFile) to read in each line of the recipe file Stop when fgets == NULL This also tacks on a \0 to the end of the line array Then use sscanf(line, “%d,%s,etc”, &var1, &var2, &var3) to parse each line for the information Would have to create new arrays to store ingredients/instructions
17
Menu After parsing, print out the menu
Keep asking for option while invalid option / not “5” Just a matter of retrieving data from each array
18
1. Print Summary For loop for number of recipes
Print out name, author, time, categories as formatted Only supposed to print a certain # of characters of the entire name/author
19
2. Print Summary by prep time
Prints recipes that take less than a certain time to make Ask user for maximum prep time Keep asking if it’s invalid (hour/min < 0, min > 59) Then for every recipe, only print the line if time <= what user gives Recommended to just convert everything to minutes when checking this
20
3. Print Summary by category
Prints recipe headers in a given category Ask user for a category Cycle through all_categories and if not found, keep asking user If found, use index to check each recipe for matching category and print
21
4. Print recipe Prints an entire recipe Ask user for ID number
If < 0 or > num_recipes, keep asking for validinput Print recipe header like in the data file Print ingredients and instructions using the arrays of indexes
22
Part 1 due next Thursday List of functions you plan to make and what they do Some basic ones you definitely should have: Read_recipes or parse_recipes Print_summary or print_recipe, etc. Main_menu function Like always, lots of opportunities for code reuse Draw out how to implement the program, then decide what functions will be needed
23
List of functions you may need
Getc Fgets Sscanf Strlen Strcmp and strncmp Strcpy and strncpy Fscanf Be sure to look these functions up as needed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.