通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。
要求实现函数:
void stringFilter(const char *pInputStr,long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr:输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“deefd” 输出:“def”
输入:“afafafaf” 输出:“af”
输入:“pppppppp” 输出:“p”
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <assert.h>
- void stringFilter(const char *pInputStr,long lInputLen, char *pOutputStr)
- {
- assert(pInputStr != NULL);
- unsigned int buffer[256] = {0};
- while(*pInputStr != '\0')
- {
- buffer[*pInputStr]++;
- pInputStr++;
- }
- int i = 0;
- while(i < 256)
- {
- if(buffer[i] > 0)
- {
- *pOutputStr = (char)i;
- pOutputStr++;
- }
- i++;
- }
- *pOutputStr = '\0';
- return;
- }
- int main()
- {
- char input_str[] = "afaffffffffffffffffffffffffffffffffffffffafaf";
- int str_len = strlen(input_str);
- char *output_str = (char *)malloc(sizeof(char) * str_len);
- if(NULL == output_str)
- {
- printf("memory malloc error\n");
- exit(1);
- }
- stringFilter(input_str, str_len, output_str);
- printf("output_str = %s\n", output_str);
- free(output_str);
- output_str = NULL;
- return 0;
- }