用C/C++实现字符串的创建,并进行查找与替换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C/C++实现字符串的创建,并进行查找与替换相关的知识,希望对你有一定的参考价值。

根据给定的元素,创建一个字符串,进行指定串的查找与替换..
如果可以,请写一下算法的要点..谢谢..
我想要的是代码。。。希望高手帮忙。。急用啊。。

我给你一个我创建的,并且非常强大的STRING类。包括两个文件一个头文件另一个是CPP文件,main()方法中教你怎么使用。
//头文件保存Cpp2.h
#include <string.h>
#include <iostream.h>

class STRING

private:
char* m_str;
public:

STRING(void); //默认构造函数
STRING(char *str); //带参数构造函数
STRING(const STRING &str); //复制构造函数
~STRING(void); //析构函数

STRING& operator = (const STRING& other); //字符串复制
STRING& operator = (const char* str); //字符串赋值

STRING& operator + (const STRING& other); //字符串连接
STRING& operator + (const char *str); //字符串连接
bool operator == (const STRING& other) const; //字符串比较
bool operator == (const char* str) const; //字符串比较
char& operator [](int i); //取字符串位

char* GetBuffer(void); //字符串
int GetLen(void); //取长度
char* GetLeft(int len); //从左边取长度为len的字符串
char* GetRight(int len); //从右边取长度为len的字符串
char* SubString(int len, int start= 0); //从start位置取长度len的字符串
int GetIndex(char*search, int start = 0); //字符串搜索,返回第一个子串在m_str中的位置,从0开始数
int Replace(char *search, char *replace); //search被替换的字符串, replace替换的字符串, 返回被替换的次数

void Reverse(void); //字符串反转
void print(void); //打印字符串
;
//CPP文件保存名为Cpp2.cpp
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include<iostream.h>

#include "Cpp2.h"

//默认构造函数
STRING::STRING(void)

m_str = new char('\0');


//带参数构造函数
STRING::STRING(char *str)

if(m_str != NULL)

m_str = new char[strlen(str) + 1];
strcpy(m_str, str);

else

m_str = new char[1];
*m_str = '\0';



//copy构造函数
STRING::STRING(const STRING &str)

m_str = new char[strlen(str.m_str) + 1];
if(m_str) strcpy(m_str, str.m_str);


//析构函数
STRING::~STRING()

if(m_str != NULL) delete [] m_str;
cout << "~STRING() is called" << endl;


//赋值运算,字符串复制
STRING& STRING::operator = (const STRING &other)

if(this != &other)// 如果str不是它本身

if(m_str != NULL) delete [] m_str; //防止内存泄露
m_str = new char[strlen(other.m_str) + 1];
strcpy(m_str, other.m_str);


return *this;


//字符串复制
STRING& STRING::operator = (const char* str)

if(m_str != NULL) delete [] m_str;
m_str = new char[strlen(str) + 1];
strcpy(m_str, str);

return *this;


//加法运算,字符串连接
STRING& STRING::operator + (const STRING& other)

char *temp = new char[strlen(m_str) + strlen(other.m_str) + 1];
strcpy(temp, m_str);
delete [] m_str;
strcat(temp, other.m_str);
m_str = temp;
return *this;


//加法运算,字符串连接
STRING& STRING::operator + (const char *str)

char *q = new char[strlen(m_str) + strlen(str) + 1];
strcpy(q, m_str);
delete [] m_str;
strcat(q, str);
m_str = q;
return *this;


//字符串比较
bool STRING::operator == (const char* str) const

return strcmp(m_str, str) == 0;


//类与类比较
bool STRING::operator == (const STRING& other) const

if(this == &other) return true; // this 指针指向 other 对象
return strcmp(m_str, other.m_str) == 0;


//[]操作符重载,取下标为i的字符
char& STRING::operator [](int i)

return this->m_str[i];


//取字符串
char* STRING::GetBuffer(void)

return this->m_str;


//取长度
int STRING::GetLen(void)

return strlen(m_str);


//从左边取长度为len的字符串
char* STRING::GetLeft(int len)

if(len > GetLen()) return this->m_str;
char*strtmp = (char * )malloc(sizeof(char) * (len + 1));
char *p = strtmp;
memset(strtmp, 0x00, len + 1);
for(int i = 0 ; i < len; i++)

*p++ = m_str[i];

*p = '\0';
return strtmp;


//从右边取长度为len的字符串
char* STRING::GetRight(int len)

if(len > GetLen()) return this->m_str;
char*strtmp = (char * )malloc(sizeof(char) * (len + 1));
memset(strtmp, 0x00, len + 1);
char *p = strtmp + len;
*p = '\0';
for(int i = GetLen() - 1 ; i > 0 ; i--)

*(--p) = m_str[i];

return strtmp;


//从start位置取长度len的字符串
char* STRING::SubString(int start= 0, int len)

return NULL;


//字符串搜索,返回子串substr在m_str第start个字符之后的位置,若不存在,返回-1
int STRING::GetIndex(char *search, int start)

if (start < 0) return -1;
int i = start, j = 0;
int searchlen = strlen(search);
if(GetLen() - start < searchlen) return -1;
while(i < GetLen() && j < searchlen)

if(m_str[i] == search[j])

i++;
j++;

else

i = i - j + 1;
j = 0;



if(j > searchlen - 1)
return i - searchlen;
else
return -1;


//字符串替换
int STRING::Replace(char *search, char *replace)

int count = 0;
int index, start = 0;
int len = strlen(search);
STRING strep;

//在目标串能找到替换串
while( (index = GetIndex(search, start)) > 0)

strep = GetLeft(index);//取左边的字符串
strep = strep + replace;//连接替换的字符串
strep = strep + GetRight(GetLen() - index - strlen(search));//连接被替换之后的字符
start = index + strlen(replace);//设置下一个搜索位置
*this = strep;
count++;//替换的次数增加1

return count;


//字符串反转
void STRING::Reverse(void)

char *temp, *p, *q;
int count = strlen(m_str);
temp = new char[strlen(m_str) + 1];
q = temp;
p = &m_str[count - 1];
while(count-- > 0) *q++ = *p--;
*q = '\0';
delete []m_str;
m_str = temp;


//打印字符串
void STRING::print(void)

char *p = m_str;
while(*p != '\0') cout << *p++;
cout<<endl;

void main()

STRING ss = "ababcabcdabc";
char*p = "abc";
int count = ss.Replace("abc", "1234");
printf("替换%d次, 替换后字符串:%s\n", count, ss.GetBuffer());
getchar();
参考技术A 指定串的查找,
指定串用数组a存放,
遍历目标串,将每一个值与a[0]比较,
不相等:继续遍历
相等:
进入内部循环(从数组a的第一个成员和当前指针指向的字符串的成员开始向后遍历),
-如果中途一旦发现有字符不等,break退出,从当前指向目标串的指针的下一个字符继续遍历
-当遍历到数组a结束时,都相等,输出,并将当前指向目标串的指针跳过sizeof(a)的长度后继续遍历

指定串的替换
在上面的查找的基础上,
-当遍历到数组a结束时,都相等,将目标串的当前指针的前sizeof(a)的成员到当前指向成员替换成新的字符
参考技术B //用标准库,很简单,一看就明
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()

int strMax = 0;
string str;
cout << "字符串数目: " << ends;
cin >> strMax;

vector<string> vecStr;
for (int i = 0; i != strMax; ++i)

cin >> str;
vecStr.push_back(str);


cout << "查找串: " << ends;
cin >> str;
bool sign = false;
vector<string>::iterator iter;
for (iter = vecStr.begin(); iter != vecStr.end(); ++iter)

if (*iter == str)

cout << "串 " << str << "找到." << endl;
cout << "替换串为: " << ends;
cin >> str;
*iter = str;
sign = true;


if (sign == false)
cout << "找不到串: " << str << endl;
else
for (iter = vecStr.begin(); iter != vecStr.end(); ++iter)

cout << *iter << endl;


return 0;
本回答被提问者采纳
参考技术C #include <iostream>
#include <string>
using namespace std;

int main()


string str = "abdaetglljlj";/////指定串,可根据要求替换
string substr = "lj";////要查找的串,可根据要求替换
int pos;
pos = str.find(substr);////查找指定的串
while (pos != -1)

str.replace(pos,substr.length(),"aa");////用新的串替换掉指定的串
pos = str.find(substr);//////继续查找指定的串,直到所有的都找到为止



cout<<str<<endl;
return 0;
参考技术D #include <iostream>
using namespace std;

int GetStringPos(char* str1, char* str2 )

int j=0;
for(int i=0; i<strlen(str1); i++)

if(str1[i]==str2[j])

j++;
if(j==strlen(str2))
return i-strlen(str2)+1;

else
j=0;


return -1; //在母字符串找不到子字符串

void Replace(char* str, char *find, char *replace)

char* pStr = new char[strlen(str)];
int nIndex = 0;
while(1)

nIndex = GetStringPos(str, find); //获取要查找字符串的位置
if(nIndex == -1) break;
strcpy(pStr, nIndex + str + strlen(find));
strcpy(nIndex + str, replace);
strcpy(nIndex + str + strlen(replace), pStr);


delete[] pStr;

int main()

char str[100] = "CIW__BLUE";
Replace(str, "_", "ABC");
cout<<str<<endl;
return 0;

C语言试题190之实现函数在第一个参数中进行查找,并返回匹配第二个参数所包含的字符的数目

以上是关于用C/C++实现字符串的创建,并进行查找与替换的主要内容,如果未能解决你的问题,请参考以下文章

C语言编写数据结构查找算法

c语言中如何在一个字符串中查找/出现的位置?需要第一次出现和第二次出现中间的内容和第二次出现和第三

c#C/S实现文件的上传下载

C语言文件中字符串的查找与替换

用C语言实现 原字符串中指定的子串的的查找与替换代码?注:一定要有用户自己输入指定子串的那个过程!

C语言试题190之实现函数在第一个参数中进行查找,并返回匹配第二个参数所包含的字符的数目