Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
replacing pi word with 3.14 .it is showing the output but forcing me to close the program,any help regarding this will be appreciated.

What I have tried:

C++
#include <iostream>
#include <vector>
#include <string>
#include<cstring>
#include <sstream>
using namespace std;
void swa(char b[]);
int main()
{
    char str[]="pi day is celebrated in march, pi day is on 14 march";
    swa(str);
}
void  swa(char b[])
{
    int i;
    char * str=new char();
    str=strtok(b," ");
    char *ptr1=new char();
    strcat(ptr1,str);
    strcat(ptr1," ");

    if(strcmp(str,"pi")==0)
    {
        ptr1="3.14 ";
       // cout<<ptr1<<" ";
    }
    if(str!=NULL)
    {
        cout<<ptr1;
        swa(b+strlen(str)+1);
    }
}
Posted
Updated 5-Jul-19 18:56pm
Comments
Afzaal Ahmad Zeeshan 13-Jun-17 4:48am    
You are not replacing the PI with 3.14 at all.

You can try using the replace function to replace the characters in a string with a new set of characters (strings).
Nilesh Sinha 13-Jun-17 13:16pm    
My question is basically write a replace function instead of using in-built replace function.

There are a load of problems here, starting with prt1 not containing enough space for the data you are concatenating into it, but more importantly, you aren't using strtok even close to correctly.
Look at the documentation: strtok - C++ Reference[^] - it includes an example of how to use it. Throw that code away, and start again once you have read the documents and understand what you are doing, instead of just guessing and hoping for the best!
 
Share this answer
 
Comments
Nilesh Sinha 13-Jun-17 13:19pm    
Thank you for providing the reference,i look into this code once again.
Why do you use C-like string in C++ code?
Try:
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s = "pi day is celebrated in march, pi day is on 14 march";

  size_t pos = 0;

  while ( (pos = s.find("pi", pos)) != string::npos )
  {
    s.replace(pos,2, "3.14");
  }

  cout << s << endl;
}
 
Share this answer
 
Comments
Nilesh Sinha 13-Jun-17 13:19pm    
thank you very much :)
CPallini 14-Jun-17 8:15am    
You are welcome.
#include<bits/stdc++.h>
using namespace std;
void replace(char input[],int start) 
{
	if(input[start]=='\0'||input[start+1]=='\0') // base condition if string 
                  return;             //contains no element or 1 element
                                       // there won't be 
                                      //any pi as               
                       
				    // pi contains 2 characters p i
		
replace(input,start+1);//Initially start would be at 0 and increment start
                      // by 1 in each 
                      //case to traverse whole string.
	
	if(input[start]=='p' && input[start+1]=='i')
	{
		for(int j=strlen(input)-1 ;j>=start+2 ;j--)
		{
			input[j+2]=input[j]; //Shift elements by two towards right as 
                                            //3.14 contains 2 more character than 
                                            //pi
		}
		input[start]='3'; 
		input[start+1]='.';
		input[start+2]='1';
		input[start+3]='4';
		input[strlen(input)+2]='\0';
	}
}
void replacePi(char input[])
{
	replace(input,0); 
}

int main()
{
char a[1000000];
cin>>a;
replacePi(a);
cout<<a;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900