您的位置:首页 > 其它

华为机试——重复字符过滤(stringFilter)

2013-08-04 16:24 232 查看
/*
一、题目描述(60分):
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。

要求实现函数:
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr:  输入字符串
lInputLen:  输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例
输入:“deefd”        输出:“def”
输入:“afafafaf”     输出:“af”
输入:“pppppppp”     输出:“p”
*/
#include <iostream>
using namespace std;

void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{
bool flag[26] = {0};
int j = 0;
for(int i = 0;i < lInputLen;i++)
{
char temp = pInputStr[i];
if(flag[temp - 'a'] == 0)//pInputStr[i]第一次出现.
{
pOutputStr[j] = temp;
j++;//移动到下一个空格.
flag[temp - 'a'] = 1;
}
}
pOutputStr[j] = '\0';
}

int main()
{
char input[20] = "deefd";
char output[20];
stringFilter(input, 20, output);
cout << output << endl;

char input1[20] = "afafafaf";
char output1[20];
stringFilter(input1, 20, output1);
cout << output1 << endl;

char input2[20] = "pppppppp";
char output2[20];
stringFilter(input2, 20, output2);
cout << output2 << endl;

return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: