/* * Program to count upper case characters in string. * No user-provided input for now, only hard-coded test input. */ #include #include /* has isupper() */ /* count upper-case characters in string s */ /* notice that we could declare the parameter as "char * s" instead */ int count_upper_case(char s[]) { int i; int count = 0; for (i = 0; s[i] != '\0'; ++i) { if (isupper(s[i])) ++count; } return count; } /* test count_upper_case function (print s, count_upper_case(s)) */ void test_count_upper_case(char s[]) { printf("%d uppercase characters in %s\n", count_upper_case(s), s); } /* main program */ int main(void) { /* * not specifying sizes of these arrays means the compiler * will make them just big enough */ char s1[] = "hello world"; char s2[] = "HELLO world"; /* first test with some strings stored in variables */ test_count_upper_case(s1); test_count_upper_case(s2); /* also test with strings passed as literals */ test_count_upper_case("Hello World"); test_count_upper_case("1234567890"); return 0; }