//字符串过滤函数
#include<stdio.h>
int main()
{
const char stringin[100]={"aaabbbcccdddeaa"};
char stringout[24]={0};/*此处用零初始化,否则字符串处理后余下的未初始化字符元素会出现乱码。*/
char stringin1[100]={0};
char stringout1[24]={0};
stringFilter(stringin,stringout);
printf("%s\n",stringin);
printf("%s\n",stringout);
printf("Please intput the string:\n");
scanf("%s",stringin1);
stringFilter(stringin1,stringout1);
//printf("%s\n",stringin1);
printf("%s\n",stringout1);
return 0;
}
stringFilter(const char* stringInput,char* stringOut)
{
#ifdef DEBUG
if(NULL==stringInput)
return 0;
#endif
const char* instring=stringInput;
char* outstring=stringOut;
int az[26]={0};/*中间变量,比如字符a存进az[0],存完后az[i]必然大于1//如果大于1,说明az该字符已经出现*/
while(*instring!='\0')
{
if(az[*instring-'a']>0)
{
*instring++;
}
else
{ az[*instring-'a']++;
*outstring =*instring;
instring++;
outstring++;
}
}
}
//执行结果输入aaabbbcccddde
//输出 abcde
字符串过滤函数
最新推荐文章于 2024-03-05 20:01:39 发布