C String Operations

C String Operations - • • char*strip(const char*s const...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
C String Operations: Search To search str for the first occurrence of c and return a pointer to this occurrence; ( NULL if not found): char *strchr(const char *str, int c); To look for the last occurrence of c: char *strrchr(const char *str, int c);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
9: C String Operations: search To search str for the first occurrence of any character that does appear in set : size_t strcspn(const char *str, const char *set); return the length of the longest prefix of str that has been skipped (or spanned ): strcspn(" Ja va after", ”rvx") returns 2. To search str for the first occurrence of any character that does not appear in set :
Background image of page 2
strip /* strip from s leading and trailing characters from * set. For example: * char *p = strip(" ,hi, how are you,", " ,");
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: • */ • char *strip(const char *s, const char *set) { • int start = strspn(s, set); /* leading characters */ • int end; /* trailing characters */ • char *kopy; • int length = strlen(s); • if(length != start) { /* there are chars not in s */ • for(end = length; end > 1; end--) /* trailing */ • if(strchr(set, s[end]) == NULL) strip • /*char *strip() continued */ • if((kopy = calloc(length + 1, sizeof(char)))==NULL) • return NULL; • memcpy(kopy, s + start, length); • kopy[length] = '\0'; • } /* length != start */ • else { /* here, no characters in s */ • if((kopy = calloc(length + 1, sizeof(char)))==NULL) • return NULL; • strcpy(kopy, s); • } • return kopy;...
View Full Document

This note was uploaded on 02/18/2012 for the course CS 240 taught by Professor Rego during the Fall '08 term at Purdue.

Page1 / 4

C String Operations - • • char*strip(const char*s const...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online