Yêu cầu:
- Viết hàm đếm số lần xuất hiện kí tự c trong chuỗi s
Giải thuật:
- Duyệt chuỗi kí tự và so sánh với kí tự c
/*******The number of character occurrence in s************* * When Who What * 03/05/2016 vncoding create ***********************************************************/ #include <conio.h> #include <stdio.h> int cntChar(const char* s, char c); void main() { char s[] = "vncoding blog for every one"; char c = 'o'; printf("The number of \'%c\' = %d", c, cntChar(s, c)); getch(); } /********************************************* Function : cntChar() Parameter: [IN] s: string [IN] c: characeter Return : the number of occurrence of c in s **********************************************/ int cntChar(const char* s, char c) { int idx, cnt = 0; for (idx = 0; s[idx] != NULL; idx++) { if (s[idx] == c) { cnt++; } } return cnt; }
Kết quả:
Leave a Reply
You must be logged in to post a comment.