/* * Silly program to illustrate working with strings in different ways, * in particular finding their length. * (For illustration purposes only, since in practice one would almost * surely just use library function strlen().) */ #include int length_a(char s[]) { int count = 0; while (s[count] != '\0') ++count; return count; } int length_p(char *s) { char * p = s; while (*p != '\0') ++p; return p-s; } int length_r(char *s) { if (*s == '\0') return 0; else return 1 + length_r(s+1); } void test_it(char *s) { printf("for string '%s', lengths are %d, %d, %d\n", s, length_a(s), length_p(s), length_r(s)); } int main(void) { test_it("hello world"); test_it(""); test_it("a"); return 0; }