training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

mystrsrch.c (3990B)


      1 /* Exercise 8.31 */
      2 
      3 #include <stdio.h>
      4 #include <string.h>
      5 
      6 char *strchr(const char *s, int c);
      7 size_t strcspn(const char *s1, const char *s2);
      8 size_t strspn(const char *s1, const char *s2);
      9 char *strpbrk(const char *s1, const char *s2);
     10 char *strrchr(const char *s, int c);
     11 char *strstr(const char *S1, const char *s2);
     12 char *strtok(char *s1, const char *s2);
     13 
     14 int main(void)
     15 {
     16    char string[] = "yet another test string";
     17    char match[] = "not";
     18    char search = 'n';
     19    char *p;
     20    int size;
     21  
     22    p = strchr(string, search);
     23    printf("strchr(string, search): %s\n", p);
     24 
     25    size = strcspn(string, match); 
     26    printf("strcspn(string, match): %d\n", size);
     27 
     28    size = strspn(string, match); 
     29    printf("strspn(string, match): %d\n", size);
     30 
     31    p = strpbrk(string, match);
     32    printf("strpbrk(string, match): %s\n", p);
     33 
     34    p = strrchr(string, search);
     35    printf("strrchr(string, search): %s\n", p);
     36 
     37    p = strstr(string, match);
     38    printf("strstr(string, match): %s\n", p);
     39 
     40    printf("\nTokenize..\n");
     41    p = strtok(string, " ");
     42    while( p != NULL ) {
     43       printf("strtok(string, \" \"): %s\n", p);
     44       p = strtok(NULL, " ");
     45    }
     46 
     47    return 0;
     48 }
     49 
     50 /* locate the first occurrence of c to s and returns
     51  * a pointer to c if will be found or NULL if not */
     52 char *strchr(const char *s, int c)
     53 {
     54    int i;
     55 
     56    for(i = 0; i < (int)strlen(s); i++) {
     57       if(s[i] == c)
     58 	 return (char *)&s[i];
     59    }
     60 
     61    return NULL;
     62 
     63 } /* eof strchr() */ 
     64 
     65 /* returns the size of initial segment of s1 which
     66  * don't contains nobody of the characters in s2 */
     67 size_t strcspn(const char *s1, const char *s2)
     68 {
     69    int i, j;
     70 
     71    for(i = 0; s1[i] != '\0'; i++) {
     72       for(j = 0; s2[j] != '\0'; j++) {
     73 
     74 	 if(s1[i] == s2[j])
     75 	    return i + 1;
     76 
     77       }
     78    }
     79 
     80    return -1;
     81 } /* eof strcspn() */
     82 
     83 /* returns the size of initial segment of s1 which
     84  * contains only the characters in the string s2 */
     85 size_t strspn(const char *s1, const char *s2)
     86 {
     87    int i, j, f;
     88 
     89    for(i = 0, f = 1; s1[i] != '\0' && f; i++) {
     90 
     91       f = 0;
     92 
     93       for(j = 0; s2[j] != '\0'; j++) {
     94 	 if(s1[i] == s2[j]) {
     95 	    f = 1;
     96 	    break;
     97 	 }
     98       }
     99    }
    100 
    101    return i - 1;
    102 } /* eof strspn() */
    103 
    104 /* locate to s1 one of the characters
    105  * in s2 and returns a pointer to it */
    106 char *strpbrk(const char *s1, const char *s2)
    107 {
    108    int i, j;
    109 
    110    for(i = 0; s1[i] != '\0'; i++) {
    111       for(j = 0; s2[j] != '\0'; j++) {
    112 	 if(s1[i] == s2[j])
    113 	    return (char *)&s1[i];
    114       }
    115    }
    116 
    117    return NULL;
    118 } /* eof strpbrk() */
    119 
    120 /* locate the latest occurrence of c in
    121  * the string s and return its position */
    122 char *strrchr(const char *s, int c)
    123 {
    124    int i;
    125 
    126    for(i = (int)strlen(s) - 1; i >= 0; i--) {
    127       if(s[i] == c)
    128 	 return (char *)&s[i];
    129    }
    130 
    131    return NULL;
    132 } /* eof strrchr() */
    133 
    134 /* locate the string s2 to s1 and return its position */
    135 char *strstr(const char *s1, const char *s2)
    136 {
    137    int i, j;
    138 
    139    for(i = 0; s1[i] != '\0'; i++) {
    140       if(s1[i] == s2[0]) {
    141 
    142 	 for(j = 0; s2[j] != '\0'; j++) {
    143 	    if(s1[i + j] != s2[j]) 
    144 	       break;
    145 	 }
    146 
    147 	 if(j == (int)strlen(s2))
    148 	    return (char *)&s1[i];
    149 
    150       }
    151    }
    152 
    153    return NULL;
    154 } /* eof strstr() */
    155 
    156 /* tokenize s1 using the separator(s) in s2 */
    157 char *strtok(char *s1, const char *s2)
    158 {
    159    int i, f, d;
    160    static char *last; 
    161 
    162    /* To be continue.. */ 
    163    if(s1 == NULL) {
    164       if(last == NULL)
    165 	 return NULL;
    166       s1 = last;
    167 
    168    }
    169 
    170    /* loop the string */
    171    for(i = d = 0; s1[i] != '\0'; i++) {
    172 
    173       /* search the separator(s) */
    174       if(strchr(s2, s1[i]) != NULL)
    175 	 f = 1;
    176       else
    177 	 f = 0;
    178 
    179       /* search the first non-separator character */
    180       if(!d && !f) {
    181 	 s1 = &s1[i];
    182 	 d = 1; /* skip this block the next loop */
    183 	 continue;
    184       }
    185 
    186       /* check if the latest token */
    187       if(s1[i + 1] == '\0') {
    188 	 f = 1;
    189 	 ++i;
    190       }
    191 
    192       /* search "the next" separator */ 
    193       if(d && f) {
    194 	 if(s1[i] != '\0') {
    195 	    s1[i] = '\0';
    196             last = &s1[i + 1];
    197 	 }
    198 	 else
    199 	    last = NULL;
    200 	 return s1;
    201       }
    202    }
    203 
    204    return NULL;
    205 } /* eof strtok() */
    206