/* * example of working with strings (pointer version) */ #include #include /* function to count number of letters and digits in a string */ void foobar(char *s, int *num_letters, int *num_digits) { /* * local variables for counts * we could compute directly into *num_letters and *num_digits but * it seems less cumbersome to use local variables */ int count_l = 0; int count_d = 0; while (*s != '\0') { if (isalpha(*s)) ++count_l; if (isdigit(*s)) ++count_d; ++s; } *num_letters = count_l; *num_digits = count_d; } /* function to call foobar and print input, outputs */ void testit(char s[]) { int letters; int digits; foobar(s, &letters, &digits); printf("'%s' has %d letters and %d digits\n", s, letters, digits); } /* main program with some simple tests */ int main(void) { testit("hello1234"); testit("a 1 b 2 !@#$"); return 0; }