打印源码自身的C++程序

思路:先写好框架。将框架转换成字符串形式。

处理引号、换行:这两种字符一般 在字符串会转义,因此替换时不能直接写,要用ASCII值表示。

#include <iostream>
using namespace std;

string replace_all(string src, char tc, string const & w)
{
  string s(src);
  string t(1, tc);
  unsigned int pos = s.find(t), t_size = t.size(), r_size = w.size();
  while(pos != std::string::npos){
    s.replace(pos, t_size, w);
    pos = s.find(t, pos + r_size );
  }
  return s;
}

int main()
{
  char const *s = "#include <iostream>\nusing namespace std;\n\nstring replace_all(string src, char tc, string const & w)\n{\n  string s(src);\n  string t(1, tc);\n  unsigned int pos = s.find(t), t_size = t.size(), r_size = w.size();\n  while(pos != std::string::npos){\n    s.replace(pos, t_size, w);\n    pos = s.find(t, pos + r_size );\n  }\n  return s;\n}\n\nint main()\n{\n  char const *s = |@|;\n  char const q[] = {34,0};\n  char const n[] = {92,110,0};\n  cout << replace_all(replace_all(s,124,q), 64, replace_all(s, 10, n));\n}\n";
  char const q[] = {34,0};
  char const n[] = {92,110,0};
  cout << replace_all(replace_all(s,124,q), 64, replace_all(s, 10, n));
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值