zoukankan      html  css  js  c++  java
  • hdu-2027题&&gets/getchar的区别

    hdu-2027题(水题~~~)

    统计每个元音字母在字符串中出现的次数。
    Input输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。
    Output对于每个测试实例输出5行,格式如下:
    a:num1
    e:num2
    i:num3
    o:num4
    u:num5
    多个测试实例之间由一个空行隔开。

    特别注意:最后一块输出后面没有空行:)
    Sample Input

    2
    aeiou
    my name is ignatius

    Sample Output

    a:1
    e:1
    i:1
    o:1
    u:1
    
    a:2
    e:1
    i:3
    o:0
    u:1
     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 int main()
     5 {
     6     int n,n1,n2,n3,n4,n5;
     7         cin>>n;
     8         getchar();//
     9         while(n--)
    10         {    char str[1000];
    11             gets(str);
    12             int len=strlen(str);
    13             n1=0;n2=0;n3=0;n4=0;n5=0;
    14             for(int i=0;i<len;i++)
    15             {
    16                 if(str[i]=='a')
    17                 n1++;
    18                 if(str[i]=='e')
    19                 n2++;
    20                 if(str[i]=='i')
    21                 n3++;
    22                 if(str[i]=='o')
    23                 n4++;
    24                 if(str[i]=='u')
    25                 n5++;
    26             }
    27             cout<<"a:"<<n1<<endl;
    28             cout<<"e:"<<n2<<endl;
    29             cout<<"i:"<<n3<<endl;
    30             cout<<"o:"<<n4<<endl;
    31             cout<<"u:"<<n5<<endl;
    32             if(n!=0)
    33             cout<<endl;
    34         }
    35 }
     

    //get()与getchar()的区别

    gets()用于从标准输入流stdin读入一个整行(以' '或EOF)结束,写入ptr指向的字符数组,并返回这个指针;出错或遇到文件结束时则返回NULL。行末的' '从流中取出,但不写入数组。gets()不检查被写入的数组大小。

    getchar()用于从标准输入流stdin读入一个字符,并返回这个字符。如果读到文件结尾,则返回EOF。注意到EOF不能用char类型表示,所以getchar()函数返回的是一个int型的数。

    //一句话,get读入字符串(含空格--优于getline函数),而getchar读入一个(int).

    Reason:   getline用法:cout.getline(str,100,'#')(c++语言描述).大多输入未知长度,所以用get方便得多。

    2017-02-04

  • 相关阅读:
    9、Spring Boot 2.x 集成 Thymeleaf
    【专题】Spring Boot 2.x 面试题
    8、Spring Boot 2.x 服务器部署
    7、Spring Boot 2.x 集成 Redis
    6、Spring Boot 2.x 集成 MyBatis
    5、Spring Boot 2.x 启动原理解析
    4、Spring Boot 2.x 自动配置原理
    3、Spring Boot 2.x 核心技术
    2、Spring Boot 2.x 快速入门
    centOS下安装JDK1.8.60,glassfish4.1.1以及MySQL
  • 原文地址:https://www.cnblogs.com/yocichen/p/6366414.html
Copyright © 2011-2022 走看看