c++字符串替换中while和strcpy具体用法

来源:百度知道 编辑:UC知道 时间:2024/05/21 16:03:36
字符串替换、取子串函数实现2008年05月18日 星期日 16:41/*****************************************************************************************************************************
* 函数原型:char* str_rep(char *str, const char *strSearch, const char *strReplace)

* 描述:字符串替换,查找字符串,把字符串中strSearch字符串替换为strReplace
* 输入参数:三个字符串
* 输出参数:替换后的str
* 返回值:成功返回指向str的指针,失败(如传入NULL参数)返回NULL
* 分析:字符替换过程中不考虑内存溢出问题,str如果被替换后变长,要有足够空间容纳新增字符和null字符
* 改进:可以增加一个参数,传入所能容纳的最大长度,在函数里必要时判断新串是否到达最大长度
*******************************************************************************************************************************/

#include <stdio.h>
#include <string.h>

char* str_rep(char *str, const char *strSearch, const char *strReplace)
{
char *pTemp = str;
int iStrSearlen, iStrReplen, iTemp;

if (str == NULL || strSearch == NULL || strReplace == NULL)
{
return NULL;
}

iSt

怎么编得这么复杂?
看我的:
/*extracting substring*/

#include <iostream>

using namespace std;

char *subString(const char * const s, int start, int end)
{
char * pNewString = new char[end - start + 1];

int j = 0;
for (int i = start; i < end; i++, j++)
{
pNewString[j] = s[i];
}

pNewString[j] = '\0';

return pNewString;
}

int main()
{
char *s = "Atlanta, Georgia";
cout << subString(s, 0, 7) << endl;;

return 0;
}

你QQ多少.教教我好吗?我QQ435289889