17.2.1 玩转字符串示例数组链接字符串

来源:互联网 发布:php 判断是否有小数 编辑:程序博客网 时间:2024/05/21 17:13
01./*                  02.*Copyright (c) 2014,烟台大学计算机学院                  03.*All rights reserved.                  04.*文件名称: test.cpp                  05.*作 者:李晓凡                  06.*完成日期:2014年12月12日                  07.*版本号:v1.0                  08.*                  09.*问题描述:链接字符串10.*输入描述:11.*程序输出:  两个字符串连接后的字符串12.*/#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