程序员面试100题之36在字符串中删除特定的字符

本文介绍了一道编程面试题,题目要求从一个字符串中删除另一个字符串中存在的所有字符。通过创建一个哈希表来快速查找并标记目标字符,然后遍历原始字符串,跳过已标记的字符进行修改,最终输出处理后的字符串。
// 程序员面试100题之36在字符串中删除特定的字符.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
/*
* like hash table, fasten the search speed
*/
int _tmain(int argc, _TCHAR* argv[])
{
	char str1[]="they are students";//delete from str1 while the char exist in str2
	char str2[]="aeiou";
	int hashtable[26]={0};
	char c;
	for(int i2=0,c=str2[0];c!='\0';)
	{
		
		hashtable[c-'a']=1;
		c=str2[++i2];
	}
	for (int i1=0;i1<26;i1++)
	{
		cout<<hashtable[i1]<<" ";
	}
	
	char ch=str1[0];
	int i=0,j=0; 
	for (i=0,j=0;str1[i]!='\0';)//j is the bond of the final string, 
	{
		ch=str1[i];
		
		if ((ch>'z'||ch<'a')||(hashtable[ch-'a']==0))//this can be executed even ch is null, but ....
		{
			if(i!=j)
			str1[j++]=str1[i++];// when last letter because ch does not be changed so the for loop can be executed once more //it is necessary to copy in every step, otherwise it is difficult and give arise to many problems
			else
			{
				i++;j++;
			}
		}
		else
			i++;
		//while(hashtable[ch-'a']==1)// if while is here then the logic is messed up
		//{
		//	ch=str1[++i];
		//}
	}
	//system("pause");
	str1[j]='\0';
	cout<<str1;
	cout<<endl;
	/**/
	system("pause");
	return 0;
}

简单的一道题,做了2小时,太菜了。犯了简单的错误,其实也不简单,熟不熟试试就知道。随便问个问题,“你能投篮进球吗?”,你可以轻松地回答,“我能”。因为你可以投一百遍,一千遍,投一天,只要进一个求就算了。但是只让你投一次呢?在三分外呢?在真正的比赛中呢?在有防守的情况下呢?在关键时刻呢?在受干扰的时候呢?只剩一秒的时间呢?“你还能吗?”熟不熟现在就知道了。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值