统计字符

       蛋疼的奖学金评定规则,哥研究生一年学习了php,mysql,redis,各种运维知识,多种编程语言,一年半的时间天天作息时间早上7点到晚上1点,除非周末从不间断,竟然是奖学金评定的倒数第一,我擦!

题目描述:
    统计一个给定字符串中指定的字符出现的次数。
输入:
    测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到'#'时输入结束,相应的结果不要输出。
输出:
    对每个测试用例,统计第1行中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出:
    c0 n0
    c1 n1
    c2 n2
    ... 
    其中ci是第1行中第i个字符,ni是ci出现的次数。
样例输入:
I
THIS IS A TEST
i ng
this is a long test string
#
在贴出自己的代码之前,再吐槽一下,别每次都说水题水题,其实都是为了开拓一下自己的思路,比如说我每天早上AC一道题,就是为了找到工作的状态,最讨厌装比的了

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	char str1[5], str2[80];
	int len1, len2, i, j;
	int total[5];

	while(gets(str1))
	{
		if(str1[0] == '#')
		{
			break;
		}
		
		for(i = 0; i < 5; i++)
		{
			total[i] = 0;
		}
		
		gets(str2);

		len1 = strlen(str1);
		len2 = strlen(str2);

		for(i = 0; i < len1; i++)
		{
			for(j = 0; j < len2; j++)
			{
				if(str1[i] == str2[j])
				{
					total[i] +=1;
				}
			}
		}
		for(i = 0; i < len1; i++)
		{
			printf("%c %d\n",str1[i],total[i]);
		}
		memset(str1,0,len1 * sizeof(char));
		memset(str2,0,len2 * sizeof(char));
	}

	return 0;
}


posted @ 2012-09-26 09:50  java程序员填空  阅读(158)  评论(0编辑  收藏  举报