16週目アイテム2-ポインタで文字列を回転-(1.1)文字列接続


/*
 *Copyright (c)2014,              
 *All rights reserved.
 *    :C++.cpp
 *      :   
 *    :2014 12 7 
 *     :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;
}

実行結果: