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

在字符串中删除另一个字符串中出现的字符

2013-04-19 11:39 337 查看
#include<iostream>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<cstdio>
using namespace std;
int hashtable[256];
void initTable(char *substr)
{
memset(hashtable,0,sizeof(hashtable));//#include<string.h>
char *p = substr;
while(*p!='\0')
{
hashtable[*p] = 1;
p++;
}
p = NULL;
}
char *Delete(char *source, char *substr)
{
char *fast = source;
char *slow = source;
while(*fast != '\0')
{
if(hashtable[*fast] != 1)
{
*slow = *fast;
slow++;
}
fast++;
}
*slow = '\0';
return source;
}
int main()
{

char source[] = "Buyer's remorse occurs when a person purchases something on a whim,    \
only to regret the purchase later. Similarly, a woman who is attracted   \
to you may be pushed or, on her own, go too far too soon. While in   \
the moment she may indulge in her attraction and sexual arousal) only  \
to regret her feelings or actions later. Assuming you want to reach the \
seduction phase, buyer's remorse is something you want to avoid at all costs.";
char substr[] = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";

initTable(substr);//初始化table数组
cout<<Delete(source,substr)<<endl;
getchar();

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