What is your definition of 'white space'?
Frankly, I don't think I'd want to try using scanf()
to identify double white spaces; nearly every other method would be far easier.
However, if you insist on doing the not desperately sensible, then you might want to use code derived from the following:
#include <stdio.h>
#include <string.h>
int main(void)
{
int d;
char sp[3] = "";
int n;
while ((n = scanf("%d%2[ \t]", &d, sp)) > 0)
{
printf("n = %d; d = %d; sp = <<%s>>", n, d, sp);
if (n == 2 && strlen(sp) == 2)
printf(" end of group");
putchar('\n');
}
return 0;
}
The square brackets enclose a character class and the 2 before it insists on at most 2 characters from the class. You might have to worry about it reading the newline and trying to get more data to satisfy the character class - which could be resolved by removing the newline from the character class. But then it hinges on your definition of white space, and whether groups are automatically ended by a newline or not. It wouldn't hurt to reset sp[0] = '\0';
at the end of the loop.
You might, perhaps, be better off reversing the fields, to detect two spaces before a number. But that would fail in the ordinary case, so then you'd fall back on a simple "%d"
format to read the number (and if that fails, you know you got neither spaces nor a number - error). Note that %d
chews up leading white space (as defined by the standard) - all of them.
The more I look at this, the less I like 'scanf()
only. Remind me not to take a class at your university, please.