您的位置:首页 > 其它

删除字符串中任意指定的字符

2013-04-16 23:20 316 查看
声明:以下题目来自网络

思想:八位的char型字符而言,建一个大小为256的数组,把所有元素都初始化为0。然后对于字符串中每一个字符,把它的ASCII码映射成索引,把数组中该索引对应的元素设为1,这样判断是否是要删除的字符时间复杂度就成O(1)了

删除字符时,设置两个指针*pFast,*pSlow,如果*pFast指向的不是要删除的字符把*pFast指向的值赋值给*pSlow,然后两个指针都向后迁移,若*pFast指向的是删除的字符*pFast++判断下一个字符情况

#include <stdio.h>
#include <conio.h>

char TheArray[256];

void InitTheArray(const char * szFind)
{
while('\0'!=*szFind)
TheArray[*szFind++] = 1;
}

void ProcessTheString(char * szDestination)
{
char * pFast;
char * pSlow;

pFast = pSlow = szDestination;
while ('\0'!=*pFast)
{
if(0==TheArray[*pFast])
*pSlow++ = *pFast++;
else
pFast++;
}
*pSlow = '\0';
}

int main()
{
char szDes[] = "They are Students.";
char szFind[] = "aeiou";

InitTheArray(szFind);
ProcessTheString(szDes);
printf("%s", szDes);
getch();

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