Bob*_*bHU -1 c++ strcpy strncpy
这是我程序中的函数原型:
void FindRepStr(char str[], const char findStr[], const char replaceStr[]);
它发现findStr[]在str[]与更换replaceStr[].
这是我的代码:
void FindRepStr(char str[], const char findStr[], const char replaceStr[])
{
char *s = nullptr;
s = strstr(str,findStr); //s points to the first-time appear in str
char tmp[] ="";
//length equal
if(strlen(findStr)==strlen(replaceStr))
{
for(int i=0;i<strlen(findStr);i++)
{
if(replaceStr[i]=='\0' || s[i] =='\0')
break;
else
s[i] = replaceStr[i];
}
cout<<str<<endl;
}
else
{
//find shorter than replace
if(strlen(findStr)<strlen(replaceStr))
{
//!!!problem here!!!
strncpy(tmp,s,strlen(s)+1); // store the left part
for(int i=0;i<=strlen(replaceStr);i++)
{
if(replaceStr[i]=='\0') //if end of replace
{
s[i]='\0'; //make s(str) end here
break;
}
else
s[i] = replaceStr[i]; //if not end, give the value
}
}
//finder longer than replace
else
{
//...not finished yet
}
}
}
Run Code Online (Sandbox Code Playgroud)
我还没有完成这个,但是在strncpy之后,我打印了s和tmp进行测试,我发现tmp被正确复制了,但s打印出来是空的:
cout<<"s before strncpy:"<<s<<endl;
strncpy(tmp,s,strlen(s)+1);
cout<<"tmp after strncpy:"<<tmp<<endl;
cout<<"s after strncpy:"<<s<<endl;
Run Code Online (Sandbox Code Playgroud)
但是在我编写的简单测试程序中,我发现它不会被清空:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char a[]="abc";
char b[]="defgh";
cout<<"a before:"<<a<<endl;
cout<<"b before:"<<b<<endl;
strncpy(a,b,strlen(b)+1);
cout<<"a after:"<<a<<endl;
cout<<"b after:"<<b<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的计划出了什么问题?
char tmp[] ="";
Run Code Online (Sandbox Code Playgroud)
在这里,您将创建一个具有足够空间的字符数组来保存字符串文字,包括终止nul.由于字符串文字为空,因此该字符数组只包含一个字符.
如果你写的不止于此(并且你写的),那么你进入未定义行为的领域.基本上你是把你的字符串倾倒在堆栈中的随机位置; 可以预见,这并不能很好地结束.
您需要确保您的角色阵列有足够的空间来执行您想要的操作.
此外,您的程序逻辑看起来完全破碎.我没有看到该代码应该如何执行函数名称建议应该做的事情.