1 / 80

ecs30 Summer 2014: Programming and Problem Solving # 08: Strings Chapter 8

ecs30 Summer 2014: Programming and Problem Solving # 08: Strings Chapter 8. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. String Library. Table 9.1, page 455

lenci
Download Presentation

ecs30 Summer 2014: Programming and Problem Solving # 08: Strings Chapter 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ecs30 Summer 2014:Programming and Problem Solving#08: Strings Chapter 8 Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu ecs30 Winter 2012 Lecture #01

  2. String Library • Table 9.1, page 455 • strncpy, strcpy, strncat, strcat, strncmp, strcmp, strlen, strtok • fgets, gets ecs30 Winter 2012 Lecture #15

  3. $ man strcpy ecs30 Winter 2012 Lecture #15

  4. strcpy vs. strncpy char *strcpy (char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); ecs30 Winter 2012 Lecture #15

  5. strncpy(result, s2, 9) \0 bzero(result, 200); ecs30 Winter 2012 Lecture #15

  6. #include <stdio.h> #include <string.h> intmain(void) { char dest_s[20]; char src_s[20] = “Jan. 30, 1996”; fprintf(stdout, “%s\n”, strncpy(dest_s, src_s, 9)); return 0; } ecs30 Winter 2012 Lecture #15

  7. #include <stdio.h> #include <string.h> int main(void) { char dest_s[20]; char src_s[20] = “Jan. 30, 1996”; strncpy(dest_s, src_s, 9); fprintf(stdout, “%s\n”, dest_s); return 0; } ecs30 Winter 2012 Lecture #15

  8. #include <stdio.h> #include <string.h> int main(void) { char dest_s[20]; char src_s[20] = “Jan. 30, 1996”; fprintf(stdout, “%s\n”, strncpy(dest_s, src_s, 9)); return 0; } ecs30 Winter 2012 Lecture #15

  9. strcpy(result, s1) ecs30 Winter 2012 Lecture #15

  10. How to implement strcpy? Char * Strcpy(char *s1, char *s2) { char *retS = s1; while((*s2) != ‘\0’) { *s1 = *s2; s1++; s2++; } *s1 = ‘\0’; return retS; } ecs30 Winter 2012 Lecture #15

  11. S2 ‘C’ ‘A’ ‘T’ ‘\0’ S1 for(i=0; i < strlen(s2); i++) { if(&(s1[0]) == &(s2[i]) return OVERLAP; } return SEPARATE; ecs30 Winter 2012 Lecture #01

  12. if ((s1 > s2) && (s1 <= (s2 + strlen(s2)))) ecs30 Winter 2012 Lecture #01

  13. S2 ‘C’ ‘A’ ‘C’ ‘\0’ S1 ecs30 Winter 2012 Lecture #01

  14. S2 ‘C’ ‘A’ ‘C’ ‘A’ S1 ecs30 Winter 2012 Lecture #01

  15. ‘C’ ‘A’ ‘C’ ‘A’ ‘T’ ‘\0’ ecs30 Winter 2012 Lecture #01

  16. S2 ‘C’ ‘A’ ‘T’ ‘\0’ S1 ecs30 Winter 2012 Lecture #01

  17. S2 ‘C’ ‘A’ ‘T’ ‘\0’ S1 ecs30 Winter 2012 Lecture #01

  18. S2 ‘C’ ‘A’ ‘T’ ‘\0’ S1 += strlen(S2); S2 += strlen(S2); S1 S2 ‘C’ ‘A’ ‘T’ ‘\0’ S1 ecs30 Winter 2012 Lecture #01

  19. ecs30 Winter 2012 Lecture #15

  20. Result >, =, < s1 strncmp(result, s2, 6) ecs30 Winter 2012 Lecture #15

  21. strcmp(result, s1) ecs30 Winter 2012 Lecture #15

  22. Q-00 (Ordering Four Numbers, 5%) Given the following function call Order_2, please finish the function Order_4, to order/sort four numbers. Please note that you can ONLY use FIVE lines of Order_2 and nothing else. void Order_2(char *smp, char *lgp) { char *temp = malloc(sizeof(char) * (strlen(smp) + strlen(lgp)); if (strcmp(smp, lgp) > 0) { strcpy(temp, smp); strcpy(smp, lgp); strcpy(lgp, temp); } free(temp); } ecs30 summer 2014, midterm

  23. >, ==, < • strcmp ecs30 Winter 2012 Lecture #15

  24. ecs30 Winter 2012 Lecture #15

  25. #include <stdio.h> #include <string.h> int main(void) { char s1[9] = “UC “; char s2[9] = “Davis”; fprintf(stdout, “%s\n”, strcat(s1, s2)); return 0; } ecs30 Winter 2012 Lecture #15

  26. ‘U’ ‘C’ ‘ ’ ‘\0’ ? ? ? ? ? s1 ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ‘U’ ‘C’ ‘ ’ ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ s1 strcat(s1, s2); ecs30 Winter 2012 Lecture #15

  27. #include <stdio.h> #include <string.h> char * strcat(char *dest, char *src) { // 0. Get the strlen for both dest & src // 1. find out the end of the dest! // 2. find out the strlen of src! // 3. copy all the characters from src to // dest with a loop. // 4. add a ‘\0’ at the end of the dest // e.g. (dest[oSL_src + oSL_dst] = ‘\0’;) // 5. return dest; } ecs30 Winter 2012 Lecture #15

  28. ‘2’ ‘0’ ‘0’ ‘8’ ‘\0’ ? ? ? ? s1 i ecs30 Winter 2012 Lecture #15

  29. ‘2’ ‘0’ ‘0’ ‘8’ ‘\0’ ? ? ? ? s1 i i = atoi(s1); ecs30 Winter 2012 Lecture #15

  30. ‘2’ ‘0’ ‘0’ ‘8’ ‘\0’ ? ? ? ? s1 i sscanf(s1, “%d”, &i); ecs30 Winter 2012 Lecture #15

  31. Take Home… • How to implement strncpy, strcpy, strncmp, strcmp? • Likely targets for the final! ecs30 Winter 2012 Lecture #15

  32. ecs30 Winter 2012 Lecture #15

  33. ecs30 Winter 2012 Lecture #15

  34. ecs30 Winter 2012 Lecture #15

  35. ecs30 Winter 2012 Lecture #15

  36. GNU debugger • The -g flag • gdb a.out • run (r) • break (b) • next (n), nexti (ni) • step (s) • print (p) • display • continue (c) • watch • x ecs30 Winter 2012 Lecture #15

  37. gdb Examples $ script hw7_1_gdb.script • display i • c • n 2 • ni 9 • s • watch i == 3 • watch i != 0 • x /4 0xbffff862 • p i • p s • p s[14] • p s[12] = ‘\0’ • p i = 0 • b main • b strlen • b 12 http://www.digilife.be/quickreferences/QRC/GDB Quick Reference.pdf http://www.hlrs.de/organization/amt/services/tools/debugger/gdb/doc/gdb-6.3.pdf ecs30 Winter 2012 Lecture #15

  38. Recursion • A function calls itself, maybe with a different set of parameter values… • Different sets of local variables… (n-1)! N! ecs30 Winter 2012 Lecture #17

  39. (m * n) ~ (m + m * (n - 1)) ecs30 Winter 2012 Lecture #17

  40. ecs30 Winter 2012 Lecture #17

  41. It gotta stop… at some point!! Exit Strategy/Condition! ecs30 Winter 2012 Lecture #17

  42. ecs30 Winter 2012 Lecture #17

  43. ecs30 Winter 2012 Lecture #17

  44. The Towers of Hanoi http://en.wikipedia.org/wiki/Tower_of_Hanoi ecs30 Winter 2012 Lecture #17

  45. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi. Stacked upon the leftmost needle were 64 golden disks, each a different size, stacked in concentric order: ecs30 Winter 2012 Lecture #17

  46. A Legend The priests were to transfer the disks from the first needle to the second needle, using the third as necessary. But they could only moveone disk at a time, and could never put a larger disk on top of a smaller one. When they completed this task, the world would end! ecs30 Winter 2012 Lecture #17

  47. To Illustrate For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... Since we can only move one disk at a time, we move the top disk from A to B. ecs30 Winter 2012 Lecture #17

  48. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from A to C. ecs30 Winter 2012 Lecture #17

  49. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from B to C. ecs30 Winter 2012 Lecture #17

  50. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from A to B. ecs30 Winter 2012 Lecture #17

More Related