Given a string, the task is to replace all occurrences of pi with 3.14 in the given string.
Examples:
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
Approach 1:
- Create an empty output string.
- Traverse the string from the 0th index till second last index since length of pi is 2.
- If the alphabets at the current and (current+1)th index form the word “pi”, append “3.14” to the output string.
- Else append the alphabet at the current index.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string replacePi(string input)
{
string output;
int size = input.length();
for (int i = 0; i < size; ++i) {
if (i + 1 < size and input[i] == 'p' and input[i + 1] == 'i') {
output += "3.14";
i++;
}
else {
output += input[i];
}
}
return output;
}
int main()
{
string input = "2 * pi + 3 * pi = 5 * pi";
cout << replacePi(input);
return 0;
}
|
Java
class GFG {
public String replacePi(String input)
{
String output = "";
int size = input.length();
for (int i = 0; i < size; ++i) {
if (i + 1 < size && input.charAt(i) == 'p' && input.charAt(i + 1) == 'i') {
output += "3.14";
i++;
}
else {
output += input.charAt(i);
}
}
return output;
}
public static void main(String args[])
{
GFG g = new GFG();
String input = "2 * pi + 3 * pi = 5 * pi";
System.out.println(g.replacePi(input));
}
}
|
Python3
def replacePi(input):
output ="";
size = len(input);
for i in range(size):
if (i + 1 < size and input[i] == 'p' and
input[i + 1] == 'i'):
output += "3.14";
i+= 1;
else:
output += input[i];
return output;
input = "2 * pi + 3 * pi = 5 * pi";
print(replacePi(input));
|
C#
using System;
class gfg {
public string replacePi(string input)
{
string output = "";
int size = input.Length;
for (int i = 0; i < size; ++i) {
if (i + 1 < size && input[i] == 'p' && input[i + 1] == 'i') {
output += "3.14";
i++;
}
else {
output += input[i];
}
}
return output;
}
}
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;
}
}
|
Javascript
<script>
function replacePi(input) {
var output = "";
var size = input.length;
for (var i = 0; i < size; ++i) {
if (i + 1 < size && input[i] === "p"
&& input[i + 1] === "i")
{
output += "3.14";
i++;
}
else {
output += input[i];
}
}
return output;
}
var input = "2 * pi + 3 * pi = 5 * pi";
document.write(replacePi(input));
</script>
|
Output2 * 3.14 + 3 * 3.14 = 5 * 3.14
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
Approach 2: (In Java)
To replace all the PI occurrences to 3.14, use replaceAll() method of Java String Class.
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string expression = "2 * pi + 3 * pi = 5 * pi";
size_t pos = expression.find("pi");
while (pos != string::npos)
{
expression.replace(pos, 2, "3.14");
pos = expression.find("pi", pos + 1);
}
cout << expression << endl;
return 0;
}
|
Java
public class ReplacePI {
public static void main(String[] args)
{
String expression = "2 * pi + 3 * pi = 5 * pi";
expression = expression.replaceAll("pi", "3.14");
System.out.println(expression);
}
}
|
Python3
expression = "2 * pi + 3 * pi = 5 * pi"
expression = expression.replace("pi", "3.14")
print(expression)
|
C#
using System;
class Program {
static void Main() {
string expression = "2 * pi + 3 * pi = 5 * pi";
int pos = expression.IndexOf("pi");
while (pos != -1) {
expression = expression.Substring(0, pos) + "3.14" + expression.Substring(pos + 2);
pos = expression.IndexOf("pi", pos + 1);
}
Console.WriteLine(expression);
}
}
|
Javascript
let expression = "2 * pi + 3 * pi = 5 * pi";
let pos = expression.indexOf("pi");
while (pos !== -1)
{
expression = expression.slice(0, pos) + "3.14" + expression.slice(pos + 2);
pos = expression.indexOf("pi", pos + 1);
}
console.log(expression);
|
Output2 * 3.14 + 3 * 3.14 = 5 * 3.14
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(N)