C语言计算字符

#include stdio.hint main() {char s[] = TajMahal;// String Givenchar ch = a;// Character to count

编程学习网为您整理以下代码实例,主要实现:C语言计算字符,希望可以帮到各位朋友。

#include <stdio.h>

int main() {
   char s[] = "TajMahal";     // String Given
   char ch = 'a';             // Character to count

   int i = 0;
   int count = 0;             // Counter

   while(s[i] != '\0') {
      if(s[i] == ch)
         count++;

      i++;
   }

   if(count > 0) {
      if(count == 1)
         printf("%c appears %d time in '%s'", ch, count, s);
      else
         printf("%c appears %d times in '%s'", ch, count, s);
   } else
      printf("%c dID not appear in %s", ch, s);

   return 0;
}
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐