正在加载图片...
Handout #61S CS106A Summer 2002 August 12,2002 Alex Khomenko Practice Final Solutions Problem 1:Strings. /Solution 1:implement strcspn,then use that for strpbrk * int strcspn(char *cs,char *ct) { char *pos; int n 0; while (*cs !=\0'&&strchr(ct,*cs)==NULL) cs++; n++; } return n; char *strpbrk(char *cs,char *ct) char *pos; int prefixLen; prefixLen strcspn(cs,ct); if (prefixLen =strlen(cs)) return NULL; return cs prefixLen; /Solution 2:implement strpbrk,then implement strcspn * char *strpbrk(char *cs,char *ct) while (*cs !=\0') if (strchr(ct,*cs)!=NULL) return cs; else Cs++; return NULL;Handout #61S Summer 2002 August 12, 2002 Alex Khomenko Practice Final Solutions Problem 1: Strings. /* Solution 1: implement strcspn, then use that for strpbrk */ int strcspn(char *cs, char *ct) { char *pos; int n = 0; while (*cs != '\0' && strchr(ct, *cs) == NULL) { cs++; n++; } return n; } char *strpbrk(char *cs, char *ct) { char *pos; int prefixLen; prefixLen = strcspn(cs, ct); if (prefixLen == strlen(cs)) return NULL; return cs + prefixLen; } /* Solution 2: implement strpbrk, then implement strcspn */ char *strpbrk(char *cs, char *ct) { while (*cs != '\0') { if (strchr(ct, *cs) != NULL) return cs; else cs++; } return NULL; } CS106A
向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有