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

来源:互联网 发布:互盾手机数据恢复 编辑:程序博客网 时间:2024/06/06 00:33

问题及代码:

/**Copyright (c)2014,烟台大学计算机与控制工程学院*All rights reserved.*文件名称: 用指针玩字符串.cpp*作    者:白云飞*完成日期:2014年12月12日*版 本 号: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;//声明变量    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
原创粉丝点击