[ACM Steps] 1.2.8 AC Me

来源:互联网 发布:python基本命令 编辑:程序博客网 时间:2024/06/05 09:25

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1&sectionid=2&problemid=26


统计输入字符串各字符的数目:


1、gets()的用法:

从stdin流中读取字符串直至接受到换行符或EOF时停止,并将读取的结果存放在buffer指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为‘\0’空字符,并由此来结束字符串。


本函数可以无限读取,不会判断上限,所以程序员应该确保buffer的空间足够大,以便在执行读操作时不发生溢出。


用法:

char str[100];

gets(str);


2、memset()的用法

头文件:<memory.h> or <string.h>

函数介绍

void *memset(void *s, int ch, size_t n);
函数解释:将s中前n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。
注意:不要搞反 ch 与 n.

用法:memset(a,0,sizeof(a))
       


#include <stdio.h>#include <string.h>int main(){char buf[100001];int count[26];while(gets(buf)){memset(count,0,sizeof(count));int len = strlen(buf);for(int i=0;i<len;i++){if(buf[i] >= 'a' && buf[i] <='z'){count[buf[i] - 'a']++;}}for(int j=0;j<26;j++){printf("%c:%d\n", (char)('a'+j),count[j]);}printf("\n");}return 0;}


0 0
原创粉丝点击