A want to check if input string is in right format
"%d/%d"
For example, when the input will be
"3/5"
return 1;
And when the input will be
"3/5f"
return 0;
I have idea to do this using regex, but I had problem to run regex.h on windows.
How to check if input string is in correct format ... ?
A simple test is to append " %n" to a sscanf() format string to store the offset of the scan, if it got that far. Then test the offset to see if it is at the end of the string.
int n = 0;
int a, b;
// v---v----- Tolerate optional white spaces here if desired.
sscanf(s, "%d /%d %n", &a, &b, &n);
if (n > 0 && s[n] == '\0') {
printf("Success %d %d\n", a, b);
} else {
printf("Failure\n");
}
int a, b; int o1 = 0, o2 = 0, o3 = 0, o4 = 0, o5 = 0; if (sscanf(s, " %n%d/%n %n%d%n %n", &o1, &a, &o2, &o3, &b, &o4, &o5) == 2 && o1 == 0 && o2 == o3 && o4 == o5 && s[o5] == '\0') { …OK… } else { …some problem… } —— but that's pushing the outer limits of sanity. I've never gone to quite that length before!== 2.o1..o5 if you don't use the values after the if statement. I checked the code with printing after (a mild variation on) the if statement, and could have run into problems with uninitialized values in some of the error reporting if the variables were not initialized.It is not completely clear what you mean by the format "%d/%d".
If you mean that the string should be parsed exactly as if by sscanf(), allowing for 2 decimal numbers separated by a /, each possibly preceded by white space and an optional sign, you can use sscanf() this way:
#include <stdio.h>
int has_valid_format(const char *s) {
int x, y;
char c;
return sscanf(s, "%d/%d%c", &x, &y, &c) == 2;
}
If the format is correct, sscanf() will parse both integers separated by a '/' but not the extra character, thus return 2, the number of successful conversions.
Here is an alternative approach suggested by Jonathan Leffler:
#include <stdio.h>
int has_valid_format(const char *s) {
int x, y, len;
return sscanf(s, "%d/%d%n", &x, &y, &len) == 2 && s[len] == '\0';
}
If you mean to only accept digits, you could use character classes:
#include <stdio.h>
int has_valid_format(const char *s) {
int n = 0;
sscanf(s, "%*[0-9]/%*[0-9]%n", &n);
return n > 0 && !s[n];
}
%n conversion specification. int len = 0; return sscanf(s, "%d/%d%n", &x, &y, &len) == 2 && strlen(s) == len; — well, maybe the extra strlen() is a problem, but you could add a space before %n to allow trailing space in the input, and this would work easily whereas adapting the answer to do so would be relatively hard. A lot depends on as yet unspecified requirements. I note that " 3/ -6" would be acceptable with spaces before both the first and second (negative) numbers. It isn't clear whether this matters either.strlen() as you can test s[len] == '\0' instead of strlen(s) == len. Regarding trailing whitespace, both "%d/%d %c" and "%d/%d %n" allow it easily." %n" over " %c": when the desired format ends with a non-specifier as in sscanf(s, "%gC", &temperature). sscanf(s, "%gC %c", &temperature) == 1 with and without the final C. Although that case does not apply to OP's code, the " %n" suffix performs well in both cases.
char *pointer that you advance along the string, using character comparisons and perhaps<ctype.h>orstrtolto check for what you're looking for; (2)scanf, and (3) regex. All three have their pluses and minuses.strtol:char *cp = buf; int lhs = strtol(cp,&cp,10); if (*cp != '/') return 0; ++cp; int rhs = strtol(cp,&cp,10); if (*cp != 0) return 0; return 1;You can also checkerrnoafter each call.ctype.hprimitives:int hasdig = 0; for (cp = buf; *cp != 0; ++cp) { if (! isdigit(*cp)) break; hasdig = 1; } if ((*cp != '/') || (! hasdig)) return 0; ++cp; hasdig = 0; for (; *cp != 0; ++cp) { if (! isdigit(*cp)) break; hasdig = 1; } if ((*cp != 0) || (! hasdig)) return 0; return 1;