第十六周项目二——用指针玩字符串(1)

来源:互联网 发布:java什么是依赖注入 编辑:程序博客网 时间:2024/06/08 14:44
/* *Copyright  (c)  2014,烟台大学计算机学院  *All rights reserved.  *文件名称: test.cpp *作        者:陈丹  *完成日期:2014年12月15日  *版本号: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<<"用数组名作形参:"<<endl;    cout<<"s1和s2连接后:"<<s1<<endl;    cout<<"s2和s3连接后:"<<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
原创粉丝点击