第十六周上机项目二1

来源:互联网 发布:开发app软件多少钱 编辑:程序博客网 时间:2024/04/29 16:30
/* *Copyright (c) 2014, 烟台大学计算机学院 *All rights reserved. *文件名称:test.cpp *作者:陈栋梁 *完成日期:2014年 12月 16 日 *版本号:v1.0 * *问题描述: */#include <iostream>using namespace std;char *astrcat(char str1[], const char str2[]);int main(){    char s1[50]="Hello world. ";    char s2[50]="Good morning. ";    char s3[50]="vegetable bird! ";    astrcat(s1,s2);    cout<<"连接后:"<<s1<<endl;    cout<<"连接后:"<<astrcat(s2,s3)<<endl;  //返回值为char*型,可以直接显示    return 0;}//作为示例,本函数采用了形参为数组,在实现中,直接用下标法进行访问//实际上,在实现中,完全可以用指针法访问char *astrcat(char str1[], const char str2[]){    int i,j;    //请理解:以下所有str1[i]可以替换为*(str1+i),str2[j]可以……    for(i=0; str1[i]!='\0'; i++); //找到str1的结束    for(j=0; str2[j]!='\0'; i++,j++)    {        str1[i]=str2[j];    }    str1[i]='\0';//切记!!    return str1;}


运行结果:

0 0