Discuss the way to remove functions that will remove forbidden characters like [ ‘ : ’, ‘ ? ‘, ‘ ’, ‘ / ’, ‘ ’, ‘ | ’, ‘ * ’ ] from a string, for example
Input: str = “ Hello: Welco*me/ to Tu>torials point|. ”
Output: “ Hello Welcome to Tutorials point. ”
Explanation: Input String contains forbidden characters which got removed and new string has no forbidden characters.
Input: str = “ How/ are y*ou doi,ng? ”
Output: “ How are you doing ”
登录后复制
解决方案的方法
可以应用于此问题的简单方法是:
-
从任一方向遍历字符串。
-
检查每个字符是否属于禁止字符。
-
如果字符属于禁止字符,则删除该字符。
-
我们可以插入一个空值或新字符串,以插入除禁止字符外的所有字符。
示例
上述方法的C++代码
#include
#include
using namespace std;
// function to remove forbidden charcters.
void removeforbidden(char* str){
int j = 0;
int n = strlen(str);
// traversing through the string and searching for forbidden characters.
for(int i = 0;i