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++
// C++ program to replace all // pi in a given string with 3.14 #include <bits/stdc++.h> 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 |
Javascript
<script> // JavaScript 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
function replacePi(input) {
var output = "" ;
var size = input.length;
// Iterate through second last
// element of the string
for ( var 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
var input = "2 * pi + 3 * pi = 5 * pi" ;
document.write(replacePi(input));
</script> |
Output
2 * 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()
{ // Declare a string
string expression = "2 * pi + 3 * pi = 5 * pi" ;
// Call replace() method to replace PI to 3.14
size_t pos = expression.find( "pi" );
while (pos != string::npos)
{
expression.replace(pos, 2, "3.14" );
pos = expression.find( "pi" , pos + 1);
}
// Print the result
cout << expression << endl;
return 0;
} |
Java
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);
}
} |
Python3
# Declare a string expression = "2 * pi + 3 * pi = 5 * pi"
# Call replace() method to replace PI to 3.14 expression = expression.replace( "pi" , "3.14" )
# Print the result print (expression)
# This code is contributed by codebraxnzt |
C#
using System;
class Program {
static void Main() {
// Declare a string
string expression = "2 * pi + 3 * pi = 5 * pi" ;
// Call replace() method to replace PI to 3.14
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);
}
// Print the result
Console.WriteLine(expression);
}
} |
Javascript
// JavaScript equivalent code // Declare a string let expression = "2 * pi + 3 * pi = 5 * pi" ;
// Call replace() method to replace PI to 3.14 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);
} // Print the result console.log(expression); |
Output
2 * 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)