Search code examples
ccstringscanf

Checking whitespace length using sscanf


How would I verify that there is only one white space between each parameter, string, int, int in this string "string int int" using sscanf?


Solution

  • Use %n

    // "string int int"
    char dest[100];
    int d[2];
    int n[4];
    int result = sscanf(buf, "%99s%n %n%d%n %n%d", 
      dest, &n[0], &n[1], &d[0], &n[2], &n[3], &d[1]);
    if ((result == 3) && (n[1] - n[0] == 1) && (n[3] - n[2] == 1)) {
      LifeIsGood();
    }