zoukankan      html  css  js  c++  java
  • 18.11.20-C语言练习-根据输入统计字符类型

    一、题目:

    二、C程序:(注意:中文部分是程序注释,如果编译器不支持中文,需要把中文删掉)

     1 #include <stdio.h>
     2 
     3 int main() {
     4      
     5     int count_letter = 0; //保存字母数量 
     6     int count_blank = 0;  //保存空格或回车数量 
     7     int count_digit = 0;  //保存数字数量 
     8     int count_other = 0;  //保存其他符号数量 
     9     char c; //保存输入的一个字符,做临时变量使用 
    10     int i = 0; //循环变量 
    11     for(i = 0; i< 15; i++) {
    12         scanf("%c", &c); //读取用户输入的字符 
    13         if( (c>='a' && c<='z') ||  (c>='A' && c<='Z') ) {
    14             count_letter += 1;  //字母数量加一,也可以写成 count_letter++;
    15         } else if( c==' ' || c=='
    ' ) {
    16             count_blank += 1; //空格或回车数量加一 
    17         } else if( c<='9' && c>='0') {
    18             count_digit += 1; //数字数量加一 
    19         } else {
    20             count_other += 1; //其他符号数量加一 
    21         }
    22     }
    23     //以下四行是输出结果 
    24     printf("letter = %d
    ", count_letter);
    25     printf("blank = %d
    ", count_blank);
    26     printf("digit = %d
    ", count_digit);
    27     printf("other = %d
    ", count_other);
    28     return 0;
    29 }

     我用的编译器是Dev-C++,符合C语言C99标准,如果使用Visual C++ 6 编译上述程序可能会有点问题,改一下格式就行(如果再有问题,告诉我)。

    1 #include <stdio.h>
    2 
    3 void main() {//int 换成 void
    4     ......
    5     //return 0; 删除这一行
    6 }

      

    三、参考资料:

    1. ASCII码对照表:http://ascii.911cha.com/
    2. C语言中scanf函数输入回车符的问题: https://blog.csdn.net/cover_sun/article/details/52842727, https://zhidao.baidu.com/question/318275648.html
    3. C语言main函数的写法 https://blog.csdn.net/u012219371/article/details/78964904
  • 相关阅读:
    update语句中存在''语法书写方式
    CSS的代码风格
    CSS的语法规范
    CSS层叠样式表导读
    CSS简介
    HTML基本标签(下)
    HTML基本标签(上)
    HTML简介导读
    集合及其运用
    字典的镶嵌
  • 原文地址:https://www.cnblogs.com/coder211/p/10005372.html
Copyright © 2011-2022 走看看