1、
#include <stdio.h> int str_c(const char *x, int key) { int j = 0; while(*x) { if(*x == key) j++; x++; } return j; } int main(void) { char str[128]; printf("str: "); scanf("%s", str); printf("c number: %d ", str_c(str,'c')); return 0; }
2、
#include <stdio.h> #include <string.h> int put(const char *x, int key) { int i, len = strlen(x); int j = 0; for(i = 0; i < len; i++) { if(*x++ == key) j++; } return j; } int main(void) { char str[128]; printf("str: "); scanf("%s", str); printf("c number: %d ", put(str, 'c')); return 0; }