用空格替换所有出现的字符串
给定一个字符串和一个子字符串,任务是用空格替换所有出现的子字符串。我们还需要删除因此而创建的尾随和前导空格。
例子:
Input: str = “LIELIEILIEAMLIECOOL”, sub = “LIE”
Output: I AM COOL
By replacing all occurrences of Sub in Str with empty spaces, we extract the secret message as I AM COOL.
Input: str = “XYZAXYZBXYZC”, sub = “XYZ”
Output: ABC
By replacing all occurrences of Sub in Str with empty spaces, we extract the secret message as ABC.
方法:
- 在给定的字符串Str 中,将所有出现的 Sub 替换为空格。
- 删除字符串开头和结尾不需要的空格。
- 打印修改后的字符串。
下面是上述方法的实现:
C++
// C++ implementation to extract
// the secret message
#include
using namespace std;
// Trim method implementation to remove
// trailing and leading white-spaces
string trim(const string &s)
{
auto start = s.begin();
while (start != s.end() &&
isspace(*start))
start++;
auto end = s.end();
do
{
end--;
} while (distance(start, end) > 0 &&
isspace(*end));
return string(start, end + 1);
}
// Function to extract the secret message
string extractSecretMessage(string str,
string sub)
{
// Replacing all occurrences of
// Sub in Str by empty spaces
size_t pos;
while ((pos = str.find(sub)) != string::npos)
str.replace(pos, 3, " ");
// Removing unwanted spaces in the
// start and end of the string
str = trim(str);
return str;
}
// Driver code
int main(int argc, char const *argv[])
{
string str = "LIELIEILIEAMLIECOOL";
string sub = "LIE";
cout << extractSecretMessage(str, sub)
<< endl;
return 0;
}
// This code is contributed by
// sanjeev2552
Java
// Java implementation to extract the secret message
import java.io.*;
import java.util.*;
class GFG {
// Function to extract the secret message
static String extractSecretMessage(String Str, String Sub)
{
// Replacing all occurrences of
// Sub in Str by empty spaces
Str = Str.replaceAll(Sub, " ");
// Removing unwanted spaces in the
// start and end of the string
Str = Str.trim();
return Str;
}
// Driver code
public static void main(String args[])
{
String Str = "LIELIEILIEAMLIECOOL";
String Sub = "LIE";
System.out.println(extractSecretMessage(Str, Sub));
}
}
Python3
# Python3 implementation to extract
# the secret message
# Function to extract the secret message
def extractSecretMessage(Str, Sub):
# Replacing all occurrences of
# Sub in Str by empty spaces
Str= Str.replace(Sub, " ")
# Removing unwanted spaces in the
# start and end of the string
return Str.strip()
# Driver code
Str = "LIELIEILIEAMLIECOOL"
Sub = "LIE"
print(extractSecretMessage(Str, Sub))
# This code is contributed
# by ihritik
C#
// C# implementation to extract the
// secret message
using System;
class GFG
{
// Function to extract the secret message
static string extractSecretMessage(string Str,
string Sub)
{
// Replacing all occurrences of
// Sub in Str by empty spaces
Str = Str.Replace(Sub, " ");
// Removing unwanted spaces in the
// start and end of the string
Str = Str.Trim();
return Str;
}
// Driver code
public static void Main()
{
string Str = "LIELIEILIEAMLIECOOL";
string Sub = "LIE";
Console.WriteLine(extractSecretMessage(Str, Sub));
}
}
// This code is contributed by Ryuga
PHP
输出:
I AM COOL