0

I need to parse my string but my delimiters are all characters excepts a-z and A-Z. How can I do that?

I thought strtok but I must write all character as delimiter and it would be too long. Exampe string:

Ax'cda2hsa+AsF(f/a as

It needs to be split into: "Ax" "cda" "hsa" "AsF" "f" "a" "as"

Is there any parsing function in C libraries which i can write my all delimeters as an interval ?

3
  • are you really using C or is it C++? If C++ you could use Boost Regex for that. Commented Dec 11, 2011 at 8:44
  • 1
    Just scan through the string throwing all consecutive letters into a separate string and continue. Commented Dec 11, 2011 at 8:45
  • Jeff, i will do that if i can't find a suitable library function Commented Dec 11, 2011 at 8:47

3 Answers 3

1

you can just walk thru the string and do that yourself if you don't want to do regex(3)

#include <stdio.h>
#include <string.h>

int main(int ac, char *av[]) {
    char string[] = "   Ax'cda2hsa+AsF(f/a as";
    int i,idx,len;
    len = strlen(string);
    #define MAX_SPLIT 256
    #define MAX_SPLIT_MASK (MAX_SPLIT - 1)
    char buf[MAX_SPLIT];
    for (idx = 0, i=0;i<len;i++) {
        char c = string[i];
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <='Z')) {
            buf[idx++ & MAX_SPLIT_MASK] = c;
        } else {
            buf[idx++ & MAX_SPLIT_MASK] = '\0';
            if (idx > 1)
                printf("%s\n",buf);
            idx = 0;
        }
    }
    if (idx > 1) {
            buf[idx++ & MAX_SPLIT_MASK] = '\0';
            printf("%s\n",buf);
    }
    #undef MAX_SPLIT
    #undef MAX_SPLIT_MASK

    return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Why use the comparison when there is perfectly good isalpha function?
its very sensible, but i just curious is there any parsing function in C libraries which i can write my delimeters as an intervals?
Why do you want to write out the delimiters, if the characters you want to keep are from a finite set, while the delimiters can be anything?
@JoachimPileborg i just thought that there will be a need for all kinds of delimiters
@user1092043 you can make this kind of a function that takes string and will return len, and will modify a pointer you give it to point to the next valid word, and return the correct len of the word
1

If you're looking for a c library function, strspn(str, dict) is useful. It returns the length of the first substring of str containing only characters in dict. Thus,

  strspn("hello world", "leh");

Would return 4.

2 Comments

i have too much delimeters so i need to a function which i write my delimeters as an interval.
With this, you don't need delimiters. You list the acceptable letters.
0

Use strpbrk to find the start of valid characters, and then strspn to get the length. Extract into new string. Repeat until strpbrk returns NULL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.