当前位置:文章写作网 >日记 >日记 >不通过c函数,实现字符串连接功能

不通过c函数,实现字符串连接功能

2009-01-11 14:08 作者:tshfang 阅读量:5011 推荐38次 | 我要投稿

在网上看到有人发帖子:不通过c函数,实现字符串连接功能。

今天抽空研究了一下。得出结果可以实现。程序代码如下:

#include <stdio.h>

#include <stdlib.h>

void lianjie(char *p1,char *p2);/*字符串连接函数,连接后保存在p1中*/

int longth(char *p);/*判读字符串长度*/

int main()

{

char strfrom[50]="i am a good student";

char strto[20]=" i love c";

lianjie(strfrom,strto);

printf("%s",strfrom);

return 0;

}

void lianjie(char *p1,char *p2)

{

int i=0;int j=0;int k;

i=longth(p1);

j=longth(p2);

k=i+j;

for(j=0;i<k+1;j++)

{

/*i++;如果i++放到这个位置,就出了问题,

最后结果是:i am a good student。大家思考一下为什么。

*/

*(p1+i)=*(p2+j);

i++;

}

}

int longth(char *p)

{

int i=0;

for (;*(p+i);i++);/*遇到\0,循环结束*/

return i;

}

其他人在看啥

    《不通过c函数,实现字符串连接功能》的评论 (共 0 条)