440 likes | 589 Views
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) { // }.
E N D
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
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
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
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
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
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
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
#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
String Library • Table 9.1, page 455 • strncpy, strcpy, strncat, strcat, strncmp, strcmp, strlen, strtok • fgets, gets ecs30 Winter 2012 Lecture #15
$ man strcpy ecs30 Winter 2012 Lecture #15
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
strncpy(result, s2, 9) \0 bzero(result, 200); ecs30 Winter 2012 Lecture #15
#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
#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
#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
strcpy(result, s1) ecs30 Winter 2012 Lecture #15
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
Result >, =, < s1 strncmp(result, s2, 6) ecs30 Winter 2012 Lecture #15
strcmp(result, s1) ecs30 Winter 2012 Lecture #15
>, ==, < • strcmp ecs30 Winter 2012 Lecture #15
#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
‘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
#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
‘2’ ‘0’ ‘0’ ‘8’ ‘\0’ ? ? ? ? s1 i ecs30 Winter 2012 Lecture #15
‘2’ ‘0’ ‘0’ ‘8’ ‘\0’ ? ? ? ? s1 i i = atoi(s1); ecs30 Winter 2012 Lecture #15
‘2’ ‘0’ ‘0’ ‘8’ ‘\0’ ? ? ? ? s1 i sscanf(s1, “%d”, &i); ecs30 Winter 2012 Lecture #15
Take Home… • How to implement strncpy, strcpy, strncmp, strcmp? • Likely targets for the second midterm! ecs30 Winter 2012 Lecture #15
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
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
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
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
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
Recursion • A function calls itself, maybe with a different set of parameter values… • Different sets of local variables… ecs30 Winter 2012 Lecture #15
(m * n) ~ (m + m * (n - 1)) ecs30 Winter 2012 Lecture #15
It gotta stop… at some point!! Exit Strategy/Condition! ecs30 Winter 2012 Lecture #15