您的位置:首页 > 编程语言 > C语言/C++

删除字符串中指定的一些字符

2017-03-05 16:38 281 查看
给定字符串“the c programming language ”,删除字符串中和“aeum”中字符相同的字符:

char * delChs(char * strScr, char * strDel)
{
if (strScr == NULL || strDel == NULL)
{
return NULL;
}

int flag[MAX_LEN] = {0};

while (*strDel != '\0')
{
flag[*strDel] = 1;
++strDel;
}

char * temp1 = strScr;
char * temp2 = strScr;

while (*temp2 != '\0')
{
if (!flag[*temp2])
{
*temp1 = *temp2;
++temp1;
}
++temp2;
}

*temp1 = '\0';

return strScr;
}


验证程序:

int main()
{
char * str = "the c programming language";
char * del = "aeum";
char * temp = new char[strlen(str) + 1];
strcpy_s(temp, strlen(str) + 1, str);

printf("%s\n", delChs(temp, del));

delete [] temp;
temp = NULL;

return 0;
}


输出结果:

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