Problem G: 字符统计

Problem G: 字符统计

Time Limit: 1 Sec   Memory Limit: 16 MB
Submit: 1842   Solved: 1017
[ Submit][ Status][ Web Board]

Description

给出一篇英文文章,含大小写字母、数字、标点符号和空白符等,统计其中各个英文字母出现的次数和各类字符出现的次数。

统计各类字符时,应使用头文件<ctype.h>中的字符分类函数。这里用到以下5个函数:

islower(c) c是否小写字母:'a'~'z';
isupper(c) c是否大写字母:'A'~'Z';
isdigit(c) c是否数字:'0'~'9';
isspace(c) c是否空白字符:包括空格(' ')、换页符('\f')、换行符('\n')、回车符('\r')、水平制表符('\t')和垂直制表符('\v');
ispunct(c) c是否标点符号:标点符号包括除字母、数字和空白符之外的所有可打印字符,也就是说输入的所有字符减去以上4类字符数正好是标点符号数。(仅限C99)

注意,在C89中(如VC6.0),ispunct()函数的定义与C99(如本地的codeblocks和OJ后台的gcc)不同。在C89中,ispunct(c)在c为除空格符和字母、数字之外的所有可打印字符时都返回真。所以,当你使用VC6.0编译器时,使用该函数本地计算的结果可能不对,但是不影响提交后的结果正确,如果你的程序没有其他错误。

Input

输入为一篇英文文章,最少有一个字符,至EOF结束。

Output

输出各类字符的出现次数和每个字母的出现次数。按顺序,每行输出依次为:所有字符数、小写字母数、大写字母数、数字字符数、空白符数、标点符号数,然后是从A~Z所有字母出现的次数。每个字母的出现次数是大小写合并统计的。

输出格式见sample。

Sample Input

SDUSTOJ Online Judge FAQQ:What is the compiler the judge is using and what are the compiler options?A:The online judge system is running on Debian Linux. We are using GNU GCC/G++ for C/C++ compile, Free Pascal for pascal compile and sun-java-jdk1.6 for Java. The compile options are:C: gcc Main.c -o Main -O2 -Wall -lm --static -std=c99 -DONLINE_JUDGEC++: g++ Main.cc -o Main -O2 -Wall -lm --static -DONLINE_JUDGEPascal: fpc Main.pas -oMain -O1 -Co -Cr -Ct -CiJava: javac -J-Xms32m -J-Xmx256m Main.java *Java has 2 more seconds and 512M more memory when running and judging.Our compiler software version:gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5glibc 2.3.6Free Pascal Compiler version 2.4.0-2 [2010/03/06] for i386java version "1.6.0_22"Q:Where is the input and the output?A:Your program shall read input from stdin('Standard Input') and write output to stdout('Standard Output').For example,you can use 'scanf' in C or 'cin' in C++ to read from stdin,and use 'printf' in C or 'cout' in C++ to write to stdout.User programs are not allowed to open and read from/write to files, you will get a "Runtime Error" if you try to do so.Here is a sample solution for problem 1000 using C++:#include using namespace std;int main(){ int a,b; while(cin >> a >> b) cout << a+b << endl;return 0;}Here is a sample solution for problem 1000 using C:#include int main(){ int a,b; while(scanf("%d %d",&a, &b) != EOF) printf("%d\n",a+b);return 0;}Here is a sample solution for problem 1000 using PASCAL:program p1001(Input,Output); var a,b:Integer; begin while not eof(Input) do begin Readln(a,b); Writeln(a+b); end; end.Here is a sample solution for problem 1000 using Java:import java.util.*;public class Main{public static void main(String args[]){Scanner cin = new Scanner(System.in);int a, b;while (cin.hasNext()){a = cin.nextInt(); b = cin.nextInt();System.out.println(a + b);}}}

Sample Output

All Characters : 2020Lowers : 1173Uppers : 135Digits : 70Spaces : 405Puncts : 237A : 113B : 24C : 62D : 47E : 108F : 25G : 32H : 23I : 111J : 19K : 1L : 59M : 51N : 121O : 97P : 46Q : 3R : 83S : 78T : 92U : 63V : 14W : 19X : 8Y : 9Z : 0

HINT

静下心来想想,你能用数组做些什么?来简化程序的编写。否则,这么多种类需要统计,只是定义单字母的变量还不够用呢。那就太麻烦了!另外,不要试图数清楚sample的各种字符数。因为最后一行末尾的回车没显示出来,并且文中有很多对齐的地方使用的是制表符'\t',而不是空格,所以,你只能依赖自己的编程能力和构造测试数据的能力。


Append Code

//这个2020个Characters大家不要信,我用word查的a一共就112个,2020不好查。提示:wa17%:主要问题是getchar这里用的gets,我当时定义的char a【10005】wa17%了,不知道开个几十万能不能成。

#include<stdio.h>
#include<string.h>
#include<ctype.h>
char a;
int main()
{
    //freopen("in.txt","r",stdin);
       int f,l=0,i,zmlow=0,zmup=0,num=0,space=0,pun=0;
    int b[92]={0};
    while((a=getchar())!=EOF)
    {
        l++;
        //for(i=0;i<f;i++)
        {
            if(islower(a))
            {
                zmlow++;
                b[a-96]++;
            }
            else if(isupper(a))
            {
                zmup++;
                b[a-64]++;
            }
                else if(isdigit(a)) num++;
            else if(isspace(a)) space++;
            else if(ispunct(a)) pun++;
        }
        //space++;
    }
    //space--;
    //l--;
    printf("All Characters : %d\n",l);
    printf("Lowers : %d\n",zmlow);
    printf("Uppers : %d\nDigits : %d\nSpaces : %d\nPuncts : %d\n",zmup,num,space,pun);
    for(i=1;i<27;i++)
    {
        printf("%c : %d\n",i+64,b[i]);
    }
    return 0;
}
 
/**************************************************************
    Problem: 1509
    User: 201701060928
    Language: C
    Result: Accepted
    Time:8 ms
    Memory:748 kb
****************************************************************/


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这道题目要求我们将字符串中每个单词的首字母变成大写。我们可以先将字符串按照空格分割成单词,然后对每个单词进行处理。处理的方法是将单词的第一个字符转换成大写,然后再将剩余的字符拼接起来。最后将处理后的单词再拼接成一个字符串即可。 ### 回答2: 题目要求编写一个程序,将给定字符串的每个单词的首字母变为大写。 首先,我们可以使用`split()`方法将字符串分割成单词的列表。然后,遍历每个单词,将首字母转化成大写。最后,使用`join()`方法将单词列表重新连接成字符串,得到结果。 以下是一个可能的实现: ```python def capitalize_first_letter(sentence): words = sentence.split() # 将字符串分割成单词列表 result = [] for word in words: capitalized_word = word[0].upper() + word[1:] # 将首字母变为大写 result.append(capitalized_word) return ' '.join(result) # 将单词列表重新连接成字符串 # 测试示例 print(capitalize_first_letter('hello world')) # 输出: Hello World print(capitalize_first_letter('zero starting point')) # 输出: Zero Starting Point ``` 以上代码中,`capitalize_first_letter`函数接收一个字符串作为参数,并返回首字母变为大写的结果字符串。我们将字符串使用`split()`方法分割成单词列表,然后遍历每个单词,将首字母转化成大写。最后,使用`join()`方法将单词列表重新连接成字符串,并返回结果。 通过上述实现,我们可以满足题目要求,将给定字符串的每个单词的首字母变为大写。 ### 回答3: 问题g:零起点学算法106——首字母变大写 这个问题要求我们将给定字符串中每个单词的首字母变为大写。解决这个问题的方法有很多种,下面我将提供两种方法。 方法一:使用内置函数capitalize() 我们可以使用内置函数capitalize()来将字符串的首字母变为大写。首先,我们将给定的字符串按照空格分割成单词列表。然后,对于每个单词,我们使用capitalize()函数将其首字母变为大写,并将其添加到结果列表中。最后,我们将结果列表连接成一个字符串,并返回这个结果字符串作为答案。 def capitalize_words(sentence): words = sentence.split() capitalized_words = [word.capitalize() for word in words] return ' '.join(capitalized_words) 方法二:逐个字符遍历 我们也可以逐个字符遍历给定字符串,当遇到空格时,将下一个字符变为大写。为了解决字符串首字母的问题,我们可以在遍历前在字符串的首部添加一个空格。 def capitalize_words(sentence): sentence = ' ' + sentence n = len(sentence) result = '' for i in range(1, n): if sentence[i - 1] == ' ': result += sentence[i].upper() else: result += sentence[i] return result 这两种方法都可以解决问题g中的要求,使用内置函数capitalize()的方法较为简单和直观,但如果想要了解更多底层的实现细节,逐个字符遍历的方法也是一种不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值