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 <stdio.h>#include <bits/stdc++.h>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<n;i++){ switch(str[i]){ case '/': case '\': case ':': case '?': case '"': case '<': case '>': case '|': case '*': // inserting null value in place of forbidden characters. str[j] = '\0'; default: str[j++] = str[i]; } } // printing the string. for(int i = 0;i<n;i++) cout << str[i]; return;}int main(){ char str[] = "hello: welco*me/ to tu>torial?s point|."; removeforbidden(str); return 0;}
outputhello, welcome to tutorials point.
上述代码的解释在遍历字符串时,使用switch case语句来检查字符串的每个元素与case字符是否相等。
如果字符与case字符相等,则将其替换为null字符。
结论在本教程中,我们讨论了创建一个函数来删除禁止字符(如[ ‘ : ’, ‘ ? ‘, ‘ \ ’, ‘ / ’, ‘ bb0bac7f6ed6e6a724d351e68eb1f345 ’, ‘ | ’, ‘ * ’ ])。我们讨论了一种简单的方法来解决这个问题,即通过遍历字符串并将字符与禁止字符进行匹配。
我们还讨论了用于解决这个问题的c++程序,我们可以使用c、java、python等编程语言来实现。希望您会发现本教程有帮助。
以上就是c++中移除禁止字符的函数的详细内容。
