您的位置:首页 > 职场人生

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

2012-12-22 09:37 281 查看
// 程序员面试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小时,太菜了。犯了简单的错误,其实也不简单,熟不熟试试就知道。随便问个问题,“你能投篮进球吗?”,你可以轻松地回答,“我能”。因为你可以投一百遍,一千遍,投一天,只要进一个求就算了。但是只让你投一次呢?在三分外呢?在真正的比赛中呢?在有防守的情况下呢?在关键时刻呢?在受干扰的时候呢?只剩一秒的时间呢?“你还能吗?”熟不熟现在就知道了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: