I want to input words from a file which are delimited by anything that isn't a letter. Is there an easy way to do this in C similar to using the \W predefined character classes in Java?
How should I approach this?
Thanks.
I want to input words from a file which are delimited by anything that isn't a letter. Is there an easy way to do this in C similar to using the \W predefined character classes in Java?
How should I approach this?
Thanks.
You can make use of the is_
family of functions defined in <ctype.h>
.
For instance, isalpha()
will tell you if something is an alphabetic character. isspace()
will tell you if something is whitespace.
You can use fscanf
(provided you're guaranteed a max length of word)
char temp_str[1000];
char temp_w[1000]
while(fscanf(file, "%[a-zA-Z]%[^a-zA-Z]", temp_str, temp_w) != EOF) {
printf("STR: %s\n", temp_str);
}
Edit: this should do the trick, temp_str
will hold the sequence of letters and temp_w
will hold the delimiting characters inbetween
Character classes in general and \W specifically really aren't related to Java at all. They're just part of regular expression support, which is available for many languages, including GNU C. See GNU C library Regular Expressions.