首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

恳请赐教一个妙法:不用函数调用,怎么在一个数组中,插入一串字符串

2012-03-08 
恳请赐教一个妙法:不用函数调用,如何在一个数组中,插入一串字符串unsigned char outbuff[1024]void man()

恳请赐教一个妙法:不用函数调用,如何在一个数组中,插入一串字符串
unsigned char outbuff[1024];
void man()
{
  unsigned int i;
  for(i=0;i<1024;i++)
  {
  outbuff[i]=0;
  }
  outbuff[3]="{Rd,d,china,canada"; //我想在数组outbuff[3]位置处插入一串字符串
  //这样有错,有什么方法简便的吗?
  
}

[解决办法]
那就自己写函数实现了
unsigned char outbuff[1024];
int main()
{
unsigned int i;
for(i=0;i <1024;i++)
{
outbuff[i]=0;
}
const char* p="{Rd,d,china,canada";
char* pS = &outbuff[3];
while( 0 != *p){
*pS=*p;
pS++;
p++;
}
return 0;
}
[解决办法]
memcpy (outbuff[3],p,n);
[解决办法]
移动指针不会出错.通过指针修改数据才会出错.
[解决办法]
那就自己写函数实现了 
unsigned char outbuff[1024]; 
int main() 

unsigned int i; 
for(i=0;i <1024;i++) 

outbuff[i]=0; 

const char* p="{Rd,d,china,canada"; 
char* pS = &outbuff[3]; 
while( 0 != *p){ 
*pS=*p; 
pS++; 
p++; 

return 0; 
}



上面那段程序好像不是插入一段字符串,而是替换
[解决办法]
#include<iostream>

using std::cout;
using std::endl;

int main()
{
char str[1024]="jimmy_w is a boy";
char s[]=" not";
char *p=&str[10];//after the word "is"
char *end=str;
while(*end)
++end;
--end;
while(end!=p-1)
{
*(end+sizeof(s)-1)=*end;
--end;
}
char *ps=s;
while(*ps)
*p++=*ps++;
cout<<str<<endl;
system("pause");
return 0;


result:

jimmy_w is not a boy
请按任意键继续. . .
[解决办法]
我这个是插入,你看看吧
[解决办法]
jimmy_w is a boy ?
jimmy_w is not a boy ?
??????


哈哈,学习了好多,还有额外收获呢?^_^

热点排行