gcc is_natural.c
./a.out
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
#include <stdio.h> | |
const int TRUE = 1; | |
const int FALSE = 0; | |
int is_natural(char *s) { | |
if (s[0] == '\0' || s[0] == '0') return FALSE; | |
for (int pos = 0; TRUE; pos++) { | |
char ch = s[pos]; | |
if ('0' <= ch && ch <= '9') continue; | |
if (ch == '\0') break; | |
return FALSE; | |
} | |
return TRUE; | |
} | |
void assert_eq(int want, int got) { | |
if (want == got) { | |
printf("PASS\n"); | |
return; | |
} | |
printf("FAIL\n"); | |
} | |
int main(void) { | |
assert_eq(TRUE, is_natural("1")); | |
assert_eq(TRUE, is_natural("9")); | |
assert_eq(TRUE, is_natural("10")); | |
assert_eq(TRUE, is_natural("19")); | |
assert_eq(TRUE, is_natural("99")); | |
assert_eq(TRUE, is_natural("65536")); | |
assert_eq(FALSE, is_natural("0")); | |
assert_eq(FALSE, is_natural("01")); | |
assert_eq(FALSE, is_natural("-1")); | |
assert_eq(FALSE, is_natural("")); | |
assert_eq(FALSE, is_natural("a")); | |
assert_eq(FALSE, is_natural("123a")); | |
} |