How do I use strtok with every single nonalpha character as a delimeter? (C)

前端 未结 4 1693
我寻月下人不归
我寻月下人不归 2020-12-12 05:41

So I have a string:

**BOB**123(*&**blah**02938*(*&91820**FOO**

I want to be able to use strtok to deliminate each word

相关标签:
4条回答
  • 2020-12-12 05:59

    You could probably use strtok for this, but it's probably easier to roll your own. Below is an example that uses a custom struct to hold the state and the results of the tokeniser. The state is just a pointer into the string, which must be initialised with the string to tokenise.

    The result represents a substring of that string as combination of starting pointer and length. That result is not zero-terminated, so you have to take care. This approach has the benefit that the solution doesn't allocate extra memory and doesn't overwrite the original string, so unlike strtok it works on read-only strings.

    The tokeniser itself is invoked with a function that returns 1 or 0, depending on whether a new token has been found, which makes for easy loop syntax.

    Here goes:

    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>      /* for isalpha(c) */
    
    struct alpha_t {
        const char *p;      /* Pointer int string; must be initialised */
        const char *str;    /* start of current token */
        int len;            /* length of token */
    };
    
    /*
     *      Get next alpha token from string; alpha->p must be initialised
     *      to the (possible read-only) string to work on.
     */
    int next_alpha(struct alpha_t *alpha)
    {
        if (alpha->p == NULL) return 0;
    
        /* Skip non-alpha and check for end of string */
        while (*alpha->p && !isalpha(*alpha->p)) alpha->p++;
        if (*alpha->p == 0) return 0;
    
        /* Read token of alpha charactzers */
        alpha->str = alpha->p;
        while (isalpha(*alpha->p)) alpha->p++;
        alpha->len = alpha->p - alpha->str;
    
        return 1;
    }
    
    /*
     *      Example client code
     */
    int main()
    {
        char *str = "BOB123(&blah02938(*&91820FOO";
        struct alpha_t token = {str};
    
        while (next_alpha(&token)) {
            printf("'%.*s'\n", token.len, token.str);
        }
    
        return 0;   
    }
    

    This solution uses isalpha, as you already suggested. It is easily extended to other functions - you could even pass a delimiter on non-delimiter function as argument or make it part of the struct, for a customisable tokeniser.

    0 讨论(0)
  • 2020-12-12 06:07

    One approach that can make this easier is to first overwrite all non-alpha characters with spaces:

    for (char *p = str; *p; p++)
        if (!isalpha(*p)) *p = ' ';
    

    Now you can use strtok(str, " ")

    0 讨论(0)
  • 2020-12-12 06:12
    #include <stdio.h>
    #include <ctype.h>
    
    char *strtok_t(char *str, int (*test)(int ch)){
        static char *store = NULL;
        char *token;
        if(str != NULL){
            store = str;
        }
        if(store == NULL) return NULL;
        while(*store && !test(*store)){//skip delimiter
            ++store;
        }
        if(*store == '\0') return NULL;
        token=store;
        while(*store && test(*store)){
            ++store;
        }
    
        if(*store == '\0'){
            store = NULL;
        } else {
            *store++ = '\0';
        }
        return token;
    }
    
    int main(void){
        char str[128] = "BOB123(&blah02938(*&91820FOO";
        char *token;
        for(token = strtok_t(str, isalpha); token ; token = strtok_t(NULL, isalpha)){
            printf("%s\n", token);
        }
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-12 06:20

    at first, you will have to create a list of non-alpha chars :

    char *myString = "fhewuidnjkl123782107381290z890zh";
    char nonAlphachars[0xFF];
    memset(nonAlphachars, 0, 0xFF);
    int i = 0;    
    int c = 1;
    for(; c <= 0xFF; c++)
    {
       if(!isalpha(c))
       {
          nonAlphachars[i++] = c;
       }
    }
    

    this will enable you to use strtok with myString :

    char *tok = strtok(myString, nonAlphachars);
    

    Now you will only need to iterate over your tokens aaand you're done. Mind you : thats only a untested (!) draft but i guess you'll get the idea. If you want your program to be efficient : hardcode all of the nonalpha-chars in a seperate string, discard the loop altogether and use it ... ugly but VERY fast (unlike all of the other answers)

    by the way : these are all of the nonalpha chars in their numeric representation, just spot the gap and ... i'll leave that one to you ;-) :

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 91 92 93 94 95 96 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 
    
    0 讨论(0)
提交回复
热议问题