查找目标字符串中的子串,可识别大小写,大写和小写认为相同

// ComParaString.cpp : Defines the entry point for the console application.
//

#pragma  warning (disable : 4786)
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool non_case(char c1,char c2)
{
 return toupper(c1) == toupper(c2);
}

/********************************************************************
 created: 2010/01/22
 created: 22:1:2010   11:42
 filename:  D:/work/20100115/ComParaString/ComParaString.cpp
 file path: D:/work/20100115/ComParaString
 file base: ComParaString
 file ext: cpp
 author:  Victor
 
 purpose: Srcstr 目标字符串。DstString 源字符串,substring 返回的字符串, 可识别大小写字母,认为相同,如果返回值为false substring为,空

*********************************************************************/
bool SubString(string Srcstr,string DstString,string &substring)
{

 bool  flag =  false;
 
 
 if (Srcstr.size()==DstString.size()&&equal(Srcstr.begin(),Srcstr.end(),DstString.begin(),non_case))
 {
  flag = true;
  
 }
 
 for (int i = DstString.size();i>0;i--)
  
 {
  DstString.resize(i);
  string::iterator strop;
  strop = search(Srcstr.begin(),Srcstr.end(),DstString.begin(),DstString.end(),non_case);
  if (strop==Srcstr.end())
  {
   flag = false;
  }
  else
  {
   if (!flag)
   {
    size_t index  = strop-Srcstr.begin();
    substring.append(Srcstr,index,DstString.size()); 
   }
   
   flag = true;
   
  }
 }
 return flag;
}
int main(int argc, char* argv[])
{
 char *s = "adddasdfsdfas";
 char *s1 = "asdfa";
 string strdst;
 SubString(s,s1,strdst);
 cout<<strdst<<endl;

 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值