[ ' :', ' ? 와 같이 금지된 문자를 제거하는 기능을 제거하는 방법에 대해 논의하십시오. ', ' \ ', ' / ', ' <', '> ', ' | ', ' * ' ], 예를 들어
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 ”
해결책을 찾기 위한 접근 방식
이 문제에 적용할 수 있는 간단한 접근 방식은 다음과 같습니다.
-
임의의 방향에서 문자열을 탐색합니다.
-
각 캐릭터가 금지된 캐릭터에 속하는지 확인하세요.
-
금지된 문자에 속하는 경우 해당 문자를 제거합니다.
-
null 값이나 새 문자열을 삽입하여 문자 제거에 금지된 문자를 제외한 모든 문자를 삽입할 수 있습니다.
예시
위 접근 방식에 대한 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;
}
출력
Hello, Welcome to Tutorials point.
위 코드 설명
-
대소문자 전환은 문자열의 각 요소를 대소문자로 확인하는 문자열을 순회할 때 사용됩니다.
-
문자가 대소문자와 같으면 null 문자로 대체됩니다.
결론
이 튜토리얼에서는 [ ' :', ' ? ', ' \ ', ' / ', ' <', '> ', ' | ', ' * ' ] 우리는 문자열을 순회하고 금지된 문자와 문자를 일치시켜 이 문제를 해결하는 간단한 접근 방식에 대해 논의했습니다.
우리는 또한 C, Java, Python 등과 같은 프로그래밍 언어로 할 수 있는 이 문제에 대한 C++ 프로그램에 대해 논의했습니다. 이 튜토리얼이 도움이 되기를 바랍니다.