1 / 44

ecs30 Winter 2012: Programming and Problem Solving #15: Chapter 5~9 – from Loops to Strings

ecs30 Winter 2012: Programming and Problem Solving #15: Chapter 5~9 – from Loops to Strings. Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/. int strlen ( char * s );. int strlen (char *s) { // }.

chad
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving #15: Chapter 5~9 – from Loops to Strings

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 Winter 2012:Programming and Problem Solving#15: Chapter 5~9 – from Loops to Strings Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/ ecs30 Winter 2012 Lecture #15

  2. intstrlen(char * s); int strlen(char *s) { // } fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #15

  3. int strlen(char *s) { int i = 0; while (1){if(s[i] == ‘\0’) break;} return i; } fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #15

  4. int strlen(char *s) { int i = 0; while (1){if(s[i] == ‘\0’) break;} return i; } What is/are the problem(s) here? fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #15

  5. int strlen(char *s) { inti = 0; while (1) { if(s[i] == ‘\0’) break; i++; } return i; } fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #15

  6. int strlen(char *s) { int i = 0; while (1){if(s[i++] == ‘\0’) break;} return (i-1); } fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #15

  7. int strlen(char *s) { int i = 0; while (s[i++] != ‘\0’); return (i-1); } fprintf(stdout, “output: %d\n”, strlen(string_var)); output: 13 * * * * * E r r o r ‘ ‘ - ‘ ‘ \0 string_var ecs30 Winter 2012 Lecture #15

  8. #include <stdio.h> #ifdef ECS30B_DEBUG #include <string.h> #endif ECS30B_DEBUG size_t strlen(const char *s) { inti; #ifdefECS30_DEBUG fprintf(stderr, "entering strlen(%x, %s)\n", (int) s, s); fflush(stderr); #endif /* ECS30_DEBUG */ while(s[i++] != '\0'); return (i-1); } int main(void) { char string_var[] = "*****Error - "; printf("output = %d\n", strlen(string_var)); return 0; } ecs30 Winter 2012 Lecture #15

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

  10. $ man strcpy ecs30 Winter 2012 Lecture #15

  11. 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

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

  13. #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

  14. #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

  15. #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

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

  17. 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

  18. ecs30 Winter 2012 Lecture #15

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

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

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

  22. ecs30 Winter 2012 Lecture #15

  23. #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

  24. ‘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

  25. #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

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

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

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

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

  30. 0,0 0,1 0,2 0,3 char m[4][4]; 1,0 1,1 1,2 1,3 void printMatrix (char m[][4]) { m[2][1] … } 2,0 2,1 2,2 2,3 3,0 3,1 3,2 3,3 m == &(m[0][0]) m[x][y] ~ *((char *) m + x*columns + y) 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3 2,0 2,1 2,2 2,3 3,0 3,1 3,2 3,3 How many rows I should skip? ecs30 Winter 2012 Lecture #15

  31. 0,0 0,1 0,2 0,3 char m[4][4]; 1,0 1,1 1,2 1,3 void printMatrix (char m[][4]) { m[2][1] … } 2,0 2,1 2,2 2,3 3,0 3,1 3,2 3,3 m == &(m[0][0]) &(m[x][y]) ~ ((char *) m + x*columns + y) 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3 2,0 2,1 2,2 2,3 3,0 3,1 3,2 3,3 How many rows I should skip? ecs30 Winter 2012 Lecture #15

  32. m[x][y] ~ *((char *) m + x*columns + y) ((int) m) + ((x*columns) + y) * sizeof(char) m[x][y] ~ *((int *) m + x*columns + y) ((int) m) + ((x*columns) + y) * sizeof(int) ecs30 Winter 2012 Lecture #15

  33. ecs30 Winter 2012 Lecture #15

  34. ecs30 Winter 2012 Lecture #15

  35. ecs30 Winter 2012 Lecture #15

  36. ecs30 Winter 2012 Lecture #15

  37. 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

  38. 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

  39. Recursion • A function calls itself, maybe with a different set of parameter values… • Different sets of local variables… ecs30 Winter 2012 Lecture #15

  40. (m * n) ~ (m + m * (n - 1)) ecs30 Winter 2012 Lecture #15

  41. ecs30 Winter 2012 Lecture #15

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

  43. ecs30 Winter 2012 Lecture #15

  44. ecs30 Winter 2012 Lecture #15

More Related