给定字符串,任务是将给定字符串所有出现的pi替换为3.14。
例子:
Input : str = "2 * pi + 3 * pi = 5 * pi"
Output : 2 * 3.14 + 3 * 3.14 = 5 * 3.14
Input : str = "pippppiiiipi"
Output : 3.14ppp3.14iii3.14
Input : str = "xpix"
Output : x3.14x
方法1:
- 创建一个空的输出字符串。
- 因为pi的长度是2,所以从第0个索引开始遍历该字符串,直到倒数第二个索引为止。
- 如果当前索引和第(current + 1)个索引处的字母组成单词“ pi”,请在输出字符串附加“ 3.14”。
- 否则,将字母追加到当前索引处。
下面是上述方法的实现:
C++
// C++ program to replace all
// pi in a given string with 3.14
#include
using namespace std;
// Function to replace all occurrences
// of pi in a given with 3.14
string replacePi(string input)
{
string output;
int size = input.length();
// Iterate through second last
// element of the string
for (int i = 0; i < size; ++i) {
// If current and current +1 alphabets
// form the word 'pi'
// append 3.14 to output
if (i + 1 < size and input[i] == 'p' and input[i + 1] == 'i') {
output += "3.14";
i++;
}
// Append the current letter
else {
output += input[i];
}
}
// Return the output string
return output;
}
// Driver Code
int main()
{
string input = "2 * pi + 3 * pi = 5 * pi";
cout << replacePi(input);
return 0;
}
Java
// Java program to replace all
// pi in a given String with 3.14
class GFG {
// Function to replace all occurrences
// of pi in a given with 3.14
public String replacePi(String input)
{
String output = "";
int size = input.length();
// Iterate through second last
// element of the String
for (int i = 0; i < size; ++i) {
// If current and current +1 alphabets
// form the word 'pi'
// append 3.14 to output
if (i + 1 < size && input.charAt(i) == 'p' && input.charAt(i + 1) == 'i') {
output += "3.14";
i++;
}
// Append the current letter
else {
output += input.charAt(i);
}
}
// Return the output String
return output;
}
// Driver Code
public static void main(String args[])
{
GFG g = new GFG();
String input = "2 * pi + 3 * pi = 5 * pi";
System.out.println(g.replacePi(input));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python program to replace all
# pi in a given string with 3.14
# Function to replace all occurrences
# of pi in a given with 3.14
def replacePi(input):
output ="";
size = len(input);
# Iterate through second last
# element of the string
for i in range(size):
# If current and current + 1 alphabets
# form the word 'pi'
# append 3.14 to output
if (i + 1 < size and input[i] == 'p' and
input[i + 1] == 'i'):
output += "3.14";
i+= 1;
# Append the current letter
else:
output += input[i];
# Return the output string
return output;
# Driver Code
input = "2 * pi + 3 * pi = 5 * pi";
print(replacePi(input));
# This code contributed by PrinciRaj1992
C#
// C# program to replace all
// pi in a given string with 3.14
using System;
// Function to replace all occurrences
// of pi in a given with 3.14
class gfg {
public string replacePi(string input)
{
string output = "";
int size = input.Length;
// Iterate through second last
// element of the string
for (int i = 0; i < size; ++i) {
// If current and current +1 alphabets
// form the word 'pi'
// append 3.14 to output
if (i + 1 < size && input[i] == 'p' && input[i + 1] == 'i') {
output += "3.14";
i++;
}
// Append the current letter
else {
output += input[i];
}
}
// Return the output string
return output;
}
}
// Driver Code
class geek {
public static int Main()
{
gfg g = new gfg();
string input = "2 * pi + 3 * pi = 5 * pi";
Console.WriteLine(g.replacePi(input));
return 0;
}
}
// This code is contributed by Soumik
输出:
2 * 3.14 + 3 * 3.14 = 5 * 3.14
时间复杂度:O(N),其中N是给定字符串的长度。
方法2 :(在Java)
要将所有PI事件替换为3.14,请使用Java String Class的replaceAll()方法。
public class ReplacePI {
public static void main(String[] args)
{
// Declare a String
String expression = "2 * pi + 3 * pi = 5 * pi";
// Call replaceAll() method to replace PI to 3.14
expression = expression.replaceAll("pi", "3.14");
// Print the result
System.out.println(expression);
}
}
输出:
2 * 3.14 + 3 * 3.14 = 5 * 3.14