在字符串中删除特定的字符
题目:输入两个字符串,从第一个字符串中删除第二个字符串中所有的字符。
举例:输入"They are students."和"aeiou",则输出之后的第一个字符串变成"Thy r stdnts."。
答:
#include "stdafx.h" #include <iostream> using namespace std; //在字符串中删除特定的字符 void DeleteSpecialChar(char *pStr, const char *pDelStr) { const int length = 256; int hashTab[length] = {0}; while (*pDelStr != '\0') { hashTab[*pDelStr++]++; } char *pFast = pStr; char *pSlow = pStr; while (*pFast != '\0') { if (!hashTab[*pFast]) { *pSlow = *pFast; pSlow++; } pFast++; } *pSlow = '\0'; } int _tmain(int argc, _TCHAR* argv[]) { char str[] = "They are students."; char strDel[] = "aeiou"; DeleteSpecialChar(str, strDel); cout<<str<<endl; return 0; }
运行界面如下: