当前位置:编程学习 > C/C++ >>

问题九:编写函数stringcat,实现字符串的连接,程序中需要使用指针形式访问字符串

/***************************************************************
                 (C语言)
  
                                         AUTHOR:liuyongshui
                                         DATE:********
   ***************************************************************/
/*
    问题九:编写函数stringcat,
    实现字符串的连接,
    程序中需要使用指针形式访问字符串

*/

#include <stdio.h>

#define MAX 100    

char *StringCat(char *source, const char *dest);  //原函数声明

int main()
{
    char s1[MAX]="I LOVE ";
    char *s2="C++ and C language!";

    StringCat(s1, s2);   //字符串连接

    printf("%s\n", s1);

    return 0;
}


// 函数的定义

char *StringCat(char *source, const char *dest)
{
     //int i=0;
     //int j;

     while(*source++) ;    //空语句,使指针移到末尾

      *source--;   //向前移一位,因为上面结束前还向后移动一位

     while(*dest!='\0')    //当遇到'\0'时结束,此句等同于while(*dest!='\0')
     {
          *source++=*dest++;  //把dest中的值赋给source
     }
    
     return 0;
}

补充:软件开发 , C语言 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,