Advertisement
cd62131

Count Character

Dec 10th, 2013
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int str_chnum(const char str[], char c) {
  4.   int count = 0;
  5.   int i;
  6.   for (i = 0; str[i] != '\0'; i++)
  7.     if (str[i] == c) count++;
  8.   return count;
  9. }
  10.  
  11. int main(void) {
  12.   char str[128];
  13.   char ch;
  14.   printf("文字列を入力してください:");
  15.   scanf("%127[^\n]%*[^\n]", str);
  16.   getchar(); // skip "\n"
  17.   printf("検索する文字を入力してください:");
  18.   scanf("%c%*c", &ch);
  19.   printf("%cは%d個ありました。\n", ch, str_chnum(str, ch));
  20.   return 0;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement