【面试题001】求出句子中没有出现过的所有字母

假如一个句子含有所有字母,就叫做pangrams. 比如:
"A quick brown fox jumps over the lazy dog" 就是一个pangrams.

要求写一个C++函数, string getMissingLetters(const string& sA)
这里sA 代表一个输入的句子。
假如sA 不是pangrams, 那么函数应该输出所有sA缺失的字母。 输出的字母应该按字典顺序排列。

btw:
字母不区分大小写,最后输出小写字母

#include <iostream>
#include <string>
using namespace std;

string getMissingLetters(const string& sA);
int _tmain(int argc, _TCHAR* argv[])
{
	

	//string strSentence = "jumps over the lazy dog";
	string strSentence = " ";
	string missingLetters = getMissingLetters(strSentence);
	cout << missingLetters << endl;
	return 0;
}

string getMissingLetters(const string& sA)
{
	int letterFre[256] = {0};
	int occurLetterNum = 0;
	for (int i = 0;i<sA.size();i++)
	{
		char ch = sA[i];
		//如果是大写字母,先转成小写
		if (ch >= 'A' && ch <= 'Z')
		{
			ch = 'a' + (ch - 'A');
		}

		//非字母的过掉
		if (ch < 'a' || ch > 'z' )
		{
			continue;
		}

		//统计出现过的字母的个数
		if (letterFre[(int)ch] == 0)
		{
			occurLetterNum ++;
		}
		letterFre[(int)ch] ++;
	}
	//没出现的字母当然是26 - occurLetterNum
	string missLetters(26 - occurLetterNum,' ');


	//将没出现的字母们拎出来
	int count = 0;
	for (int i = 'a';i <= 'z';i++)
	{
		if (letterFre[i] == 0)
		{
			missLetters[count++] = (char)i;
		}
	}
	return missLetters;

}

转载于:https://www.cnblogs.com/speedmancs/archive/2011/06/04/2072677.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值