Chinaunix首页 | 论坛 | 博客
  • 博客访问: 99233
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1011
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-15 13:58
个人简介

普普通通一个人

文章分类

全部博文(102)

文章存档

2018年(1)

2015年(13)

2014年(88)

我的朋友

分类: C/C++

2014-02-20 13:33:07


点击(此处)折叠或打开

  1. #include
    #include
    #include


  2. int main(){
  3.     //初始化计数器
  4.     int counter = 0;
  5.     //初始化存储输入字符串的数组
  6.     char find, a[100];
  7.     //提示输入字符串
  8.     printf("enter the string: ");
  9.     //接收字符串
  10.     scanf("%s", a);
  11.     //清理缓冲区,避免第一次输入时的回车被读入进来
  12.     fflush(stdin);
  13.     //提示输入要查找的字符
  14.     printf("enter the char you want to count: ");
  15.     //接收要查找的字符
  16.     scanf("%c", &find);
  17.     //循环查找每一位上的字符是否与find相符
  18.     for (int i = 0; i < strlen(a); i++){
  19.         //如果相符,则计数器+1
  20.         if (a[i] == find)
  21.             counter += 1;
  22.     }
  23.     //打印总共有多少个find出现在了字符串中
  24.     printf("there are %d %c in this string.\n", counter, find);
  25.     return 0;
  26. }

阅读(286) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~