CS 1723, Data Structures, Exam 2, Selected Answers /* CS 1723, Exam 2, Examples with strings, Problem 1 */ #include #include #include #include int digits1(char *); int digits2(char *); void change1(char *); void change2(char *); void main(void) { char *s = (char *)malloc(20); char *t = (char *)malloc(20); char *u = (char *)malloc(20); strcpy(s, "Test 1: 4/2/1997"); printf("String #1:\"%s\", digits: %i, %i\n", s, digits1(s), digits2(s)); strcpy(t, "Test 2: stuvwxyz"); printf("String #2:\"%s\", digits: %i, %i\n", t, digits1(t), digits2(t)); strcpy(u, "Test Three: None"); printf("String #3:\"%s\", digits: %i, %i\n", u, digits1(u), digits2(u)); change1(s); printf("Changed String #1:\"%s\"\n", s); change2(t); printf("Changed String #2:\"%s\"\n", t); printf("String #3:\"%s\"\n", u); } int digits1(char *s) { int len = strlen(s); int j; int count = 0; for (j = 0; j < len; j++) if (isdigit(s[j])) count++; return count; } int digits2(char *s) { int count = 0; while (*s != '\0') /* or while(*s) */ if (isdigit(*s++)) count++; return count; } void change1(char *s) { strcpy(s, "Newstring"); } void change2(char *s) { strcpy(s, "A string too long for allocation"); } runner% cc -o exam2_1 exam2_1.c runner% exam2_1 String:"Test 1: 4/2/1997", digits: 7, 7 String:"Test 2: stuvwxyz", digits: 1, 1 String:"Test Three: None", digits: 0, 0 Changed String #1:"Newstring" Changed String #2:"A string too long for allocation" String #3:"" (NOTICE: String t is OK, but string u was trashed!) /* Weeks program, used in Problem 2, Exam2 */ #include void main(void) { char **p, **q; char *weeks[] = {"none", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", NULL}; p = weeks; printf("Printing weeks[4]: %s\n", weeks[4] ); printf("Printing weeks[2][4]: %c\n", weeks[2][4] ); printf("Printing *(weeks + 3): %s\n", *(weeks + 3) ); printf("Printing *p: %s\n", *p ); printf("Printing **p: %c\n", **p ); printf("Printing *(*(p + 1) + 4): %c\n", *(*(p + 1) + 4) ); q = &weeks[2]; printf("Printing *q: %s\n", *q ); printf("Printing **q: %c\n", **q ); } runner% cc -o exam2_2 exam2_2.c runner% exam2_2 Printing weeks[4]: Thursday Printing weeks[2][4]: d Printing *(weeks + 3): Wednesday Printing *p: none Printing **p: n Printing *(*(p + 1) + 4): a Printing *q: Tuesday Printing **q: T runner% -------------------------------------------------------------------------------- /* List program, used in Problem 3 on Exam2 */ #include #include struct lnode { char ch; struct lnode *next; }; struct lnode *insert(char c, struct lnode *p); void printlist(struct lnode *); void main(void) { char c; struct lnode *list = NULL; while ((c = getchar()) != '\n') list = insert(c, list); printlist(list); } struct lnode *insert(char c, struct lnode *p) { struct lnode *q = (struct lnode *) malloc(sizeof(struct lnode)); q -> ch = c; q -> next = p; return q; } void printlist(struct lnode *p) { while (p != NULL) { printf("%c", p -> ch); p = p -> next; } printf("\n"); } -------------------------------------------------------------------------------- runner% cc -o exam2_3 exam2_3.c runner% exam2_3 abcdefg gfedcba runner%