逆串,除了字串

本文介绍了一个使用C++编写的简单程序,该程序实现了在一个给定字符串中查找并替换指定子串的功能。通过递减遍历字符串并比较子串,程序能够有效地进行替换操作,此过程在不使用标准库函数的情况下完成。
Code:
  1. #include <iostream>   
  2. using namespace std;   
  3. void convert(char *str ,char *substr)   
  4. {   
  5.     int lenstr = strlen(str);   
  6.     int lensubstr = strlen(substr);   
  7.   
  8.     while(lenstr >= 1)   
  9.     {   
  10.         while (str[--lenstr] != substr[lensubstr-1] && lenstr >= 0)   
  11.             cout<<str[lenstr];   
  12.         int ls = lenstr;   
  13.         int issub = 0;   
  14.         while(str[lenstr] == substr[lensubstr-1-issub] && issub < lensubstr )   
  15.         {   
  16.             issub++;   
  17.             lenstr--;   
  18.         }   
  19.         if (issub == lensubstr)   
  20.             for(int i = 0 ; i < lensubstr ; i++)   
  21.                 cout<<substr[i];   
  22.         else  
  23.         {   
  24.             lenstr = ls;   
  25.             for(int i = 0 ; i < issub ;i++)   
  26.                 cout<<str[lenstr--];   
  27.         }   
  28.         lenstr++;   
  29.     }   
  30.     cout<<endl;   
  31. }   
  32. void main()   
  33. {   
  34.     convert("This is my books ! And the is yours? Right? Yes!!","is");   
  35. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值