1

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.

3 Answers3

0

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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

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

Eric Conner
  • 10,422
  • 6
  • 51
  • 67
  • Thank you very much sir. One question, should it be z instead of b? – Bob Winchester Apr 17 '11 at 19:05
  • Yea it should have been z and I had one other problem. The above will work as long as the file always starts with a sequence of letters. If not, or if you need something more robust you may want to use getc() and isalpha() – Eric Conner Apr 17 '11 at 19:16
0

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.

Don Roby
  • 40,677
  • 6
  • 91
  • 113