Unlike the numeric conversion specifiers and %s
, the %c
conversion specifier does not skip blanks. Thus, given your example inputs, the %*c
is reading the blank before the operator. You could sensibly use:
while (sscanf(matrix[i][c] + offset, " %c%d %c %c%d", &col1, &row1, &op, &col2, &row2) == 5)
...data is OK...
Since you're using an offset and were capturing where the scan ended, you would use:
while (sscanf(matrix[i][c] + offset, " %c%d %c %c%d%n",
&col1, &row1, &op, &col2, &row2, &readCharCount) == 5)
...data is OK...
Note that the %n
conversion specifier is not counted, so the test remains against 5, not 6.
Also note the careful placement of spaces in the format string. They're necessary and flexible (that would handle A1+B2
OK, as well as A1 + B2
). If you are going to allow bigger spreadsheets, you might prefer to specify:
while (sscanf(matrix[i][c] + offset, " %4[a-zA-Z]%d %c %4[a-zA-Z]%d%n",
col1, &row1, &op, col2, &row2, &readCharCount) == 5)
...data is OK...
where the type of col1
and col2
changes from a single to char col1[5]; char col2[5];
(which is why the &
was dropped, too). The scan sets allow inputs like aAa1 + BbB2
to be recognized. Because of the %d
notation, spaces are allowed between the letter or letters and the number (so the code would allow aaa 999 + bbb 888
. Avoiding that is hard; I think you'd need to process the data with two scansets:
while (sscanf(matrix[i][c] + offset, " %4[a-zA-Z]%5[0-9] %c %4[a-zA-Z]%5[0-9]%n",
col1, row1, &op, col2, row2, &readCharCount) == 5)
...data is OK...
where the types are now char row1[6]; char row2[6];
and the ampersands have been dropped again. You can then confidently convert row1
and row2
into numbers.
See also: Calc cell convertor in C for code to convert column numbers into the corresponding alphabetic codes.