Lecture32-LIN-11182019-StringIII_FileFunc.pdf - Computing I COMP.1010 Fall 2019 Lecture 32 String III File Character Functions By Sirong Lin PhD

Lecture32-LIN-11182019-StringIII_FileFunc.pdf - Computing I...

• 29

This preview shows page 1 - 10 out of 29 pages.

By: Sirong Lin, PhD Computing I University of Massachusetts Lowell COMP.1010, Fall 2019 Lecture 32: String III, File & Character Functions
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Upcoming Due Dates 2 Due Date Daily 23 11-18-2019 (M), 9:00pm Program 3 11-18-2019 (M), 9:00pm Week 12 Reading MWF, 9:00am Quiz for HW4 11-20-2019 (W), 9:00am HW 5 11-20-2019 (W), in class Daily 24 11-20-2019 (W), 9:00pm Take Home Quiz 11-20-2019 (W), BEFORE class
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Outlines Review Pointer Homework II String examples (Cont’d) Function for Characters Function for Files(II) 3
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin String in C — Example2 (Cont’d) void appendString( char str1[], char str2[], char str3[], int maxOfStr3); or void appendString( char *str1, char *str2, char *str3, int maxOfStr3); 4 function declaration
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin String in C — Example2 (Cont’d) 5 void appendString( char str1[], char str2[], char str3[], int maxOfStr3) { if (str1 == NULL || str2 == NULL ) return ; int i = 0; while (str1[i] != '\0' && i < maxOfStr3-1) { str3[i] = str1[i]; i++; } int j = 0; while (str2[j] != '\0' && i < maxOfStr3-1) { str3[i] = str2[j]; i++; j++; } str3[i] = '\0'; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin String in C — Example2 (Cont’d) maxOfStr3 indicates the size of str3. We need this variable to make sure, in general, the code will not cause buffer overflow. Line 5~8: keep copying each character in str1 to the destination string (str3); also make sure the length of str3 doesn’t exceed its capacity 6
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin String in C — Example2 (Cont’d) Line 11~16: keep copying each character in str2 to the destination string (str3); also make sure the length of str3 doesn’t exceed its capacity Line 17: write the null-character to str3 7
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin String in C — Example2 (pointer version) void appendString_pointer( char *str1, char *str2, char *str3, int maxOfStr3) { int = 0; if (str1 == NULL || str2 == NULL ) return ; while (*str1 != '\0' && i < maxOfStr3-1) { *str3 = *str1; str1++; str3++; i++; } while (*str2 != '\0' && i < maxOfStr3-1) { *str3 = *str2; str3++; str2++; i++; } *str3 = '\0' ; } 8

You've reached the end of your free preview.

Want to read all 29 pages?

• Fall '15

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern