2

I'm trying to read strings without commas and dots using fscanf().

Example input:

"Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they were perfectly normal, thank you very much."

I want to read "Mr", "Mrs" and "Dursley" each time e.g

I tried several ways to do this using optional arguments, but I failed. How can I ignore the commas and dots using fscanf()?

kaya3
  • 47,440
  • 4
  • 68
  • 97
kgd
  • 1,666
  • 2
  • 11
  • 15

1 Answers1

1

You can use the regex feature of sscanf to do this.

#include <stdio.h>


int main()
{
  char *str = "Mr. Fiddle Tim went to the mall. Mr. Kurdt was there. Mrs. Love was there also. "
    "They said hi and ate ice cream together." ;

  char res[800] = { 0 };
  sscanf( str, "%800[^.,]", res ) ;
  puts( str ) ;
}

To keep going, you will need to use the return value of sscanf (or fscanf) to identify how many characters were matched. I leave that to you.

Community
  • 1
  • 1
bobobobo
  • 64,917
  • 62
  • 258
  • 363