使用3.14重新激活pi [英] Repalce pi with 3.14

查看:155
本文介绍了使用3.14重新激活pi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用3.14替换pi字。它显示输出但强迫我关闭程序,任何关于此的帮助将不胜感激。



什么我试过了:



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:

#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);
    }
}

推荐答案

这里有很多问题,从开始prt1 没有足够的空间容纳你正在连接的数据,但更重要的是,你没有使用strtok甚至接近正确。

查看文档:< a href =http://www.cplusplus.com/reference/cstring/strtok/> strtok - C ++参考 [ ^ ] - 它包含一个如何使用它的示例。扔掉那些代码,一旦你阅读完文件并了解你在做什么就重新开始,而不仅仅是猜测和希望最好!
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!


你为什么要使用 C - 中的字符串C ++ 代码?

尝试:

Why do you use C-like string in C++ code?
Try:
#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;
}


这篇关于使用3.14重新激活pi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆