c++的字符数组替换,请高手指教~
发布网友
发布时间:2023-04-05 08:53
我来回答
共3个回答
热心网友
时间:2024-12-01 11:04
1.下面的代码是不调用任何库函数完成字符串的查找和替换的代码:
#include <iostream>
using namespace std;
// 求字符串长度函数
int strlength(char *s)
{
int count = 0;
while(*s++ != '\0') count ++;
return count;
}
// 搜索字串出现的位置
int search(char *s, char *subs, int *mark)
{
unsigned int i = 0, j = 0, f = 0, mlen = 0;
for (i = 0; i <= strlength(s) - strlength(subs); i++)
{
f = 1;
for (j = 0; j < strlength(subs); j++)
{
if (*(s + i + j) != *(subs + j))
{
f = 0;
break;
}
}
if (f)
{
*(mark+mlen) = i;
mlen ++;
i += strlength(subs) - 1;
}
}
return mlen;
}
// 替换确定位置的字串
void replaces(char *s, char *old, char *news, int position)
{
int lold = strlength(old);
int lnew = strlength(news);
int len = abs(lold - lnew);
int old_start = position, old_end = lold + old_start;
int i = 0;
if (lold > lnew)
{
for (i = 0; i < lnew; i++)
{
*(s + i + old_start) = *(news + i);
}
for (i = lnew + old_start; i < (int)strlength(s); i++)
{
*(s + i) = *(s + len + i);
}
*(s + i) = '\0';
}
else
{
for (i = strlength(s); i >= old_end; i--)
{
*(s + len + i) = *(s + i);
}
*(s + strlength(s) + len) = '\0';
for (i = 0; i < lnew; i++)
{
*(s + i + old_start) = *(news + i);
}
}
}
void main()
{
char str[100];
char substr[20];
char newstr[20];
int *mark,mlen,i = 0,j = 0;
cout << "请输入主字符串:"<< endl;
cin.getline(str, 100, '\n');
cout << "请输入要替换的字符串:"<< endl;
cin.getline(substr, 100, '\n');
cout << "请输入要替换成的字符串:" << endl;
cin.getline(newstr, 100, '\n');
mlen = strlength(str)/strlength(substr);
mark = (int *)malloc(sizeof(int) * mlen);
mlen = search(str,substr,mark);
cout << "子字符串在主字符串中出现的位置为:" << endl;
for (i = 0; i < mlen; i++)
{
cout << *(mark + i) << " ";
}
cout << endl;
for (i = 0; i < mlen; i++)
{
replaces(str,substr,newstr,*(mark +i));
for (j = i + 1; j < mlen; j++)
{
*(mark + j) = *(mark + j) + strlength(newstr) - strlength(substr);
}
}
cout << "替换后的字符串为:\n" << str << endl;
}
2.下面是调用库函数完成字符串查找替换的代码:
#include <iostream>
#include <string>
using namespace std;
void search(string str, string sub)
{
int index = str.find(sub);
cout << "子字符串在主字符串中出现的位置为:" << endl;
while(index >= 0 && index < str.length())
{
cout << index << " ";
index = str.find(sub, index + sub.length());
}
cout << endl;
}
void replaces(string &str, string sub, string news)
{
int index = str.find(sub);
while(index >= 0 && index < str.length())
{
str = str.replace(index, sub.length(), news);
index = str.find(sub, index + news.length());
}
}
void main()
{
string str = "asdfasdfasdf";
string sub = "df";
string news = "123";
search(str, sub);
replaces(str, sub, news);
cout << "替换后的字符串为:" << endl;
cout << str << endl;
}
两个程序对照着看一下,希望对你以后的学习有所帮助
热心网友
时间:2024-12-01 11:04
我刚写了个函数,用GCC调试通过,希望没bug。
两个字符数组必须有'\0'终止字符串,不然就出错。
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
//找出s2在s1中第一次出现的位置,没有则返回-1。
int findstr(char* s1, char*s2);
char a[]="asdfgzxgzx", b[]="gz";
int loc=findstr(a, b);
if (loc<0) cout << "字符串2在字符串1中不存在!" << endl;
else cout << "字符串2在字符串1中第一次出现的位置: " << loc << endl;
return 0;
}
int findstr(char* s1, char*s2) {
char *p1=s1, *p2=NULL;
while (*p1 != '\0') {
p2=s2;
while ('\0'!=*p1 && '\0'!=*p2)
if (*p2++ != *p1++) break;
if (*(p1-1)==*(p2-1) && '\0'==*p2)
return (p1-s1)-(p2-s2)+1;
//这里加不加1你说了算。
if ('\0'==*p1) return -1;
}
}
热心网友
时间:2024-12-01 11:05
#include <iostream>
using namespace std;
void search(char* a,char* b)
{
bool panan=false;
int a1=0;//记录i
for (int i=0,j=0;a[i]!=NULL;i++)
{
cout<<"222";
if ((a[i]==b[j])&&!panan)
{
if (a1==0)
{
a1=i;
}
if(b[j+1]==NULL)
{
panan=true;
break;
}
j++;
}
else
{
continue;
}
}
if (!panan)
{
cout<<"没有包含"<<endl;
}
else
{
cout<<"第"<<a1<<"个数开始包含"<<endl;
}
}
int main()
{
char str[100];//本来想定义指针然后用strlen获取长度之后NEW的,不过既然不能用,那我就定义成100的字符数组了
char sub[100];//
cout << "请输入主字符串: ";
cin>> str;
cout << "请输入子字符串: ";
cin >> sub;
search(str,sub);
return 0;
}