(c语言)连接两个字符串

来源:互联网 发布:sas统计软件价格 编辑:程序博客网 时间:2024/05/18 16:13

#include <stdio.h>

 

#define N 100

 

void input( char *a, char *b )                   //输入两个字符串

{

    printf("Input the first information:\n");

    fgets(a,N,stdin);

    printf("Input the secend information:\n");

    fgets(b,N,stdin);

}


char *my_strcat( char *a, char *b )              //连接两个字符串   

{

    char *head = a;

    while( *a != '\0' )

    {

        a++;

    }

    a--;                                         //将a从'\0'后一位移到'\0'位置上

    while( *b != '\0' )

    {

        *a = *b;

b++;

a++;

    }

    *a = '\0';

    a = head;                                    //将指针移动到a[0]的地址上

    return a;

}

 

int main()

{   

    char a[N] = {0};

    char b[N] = {0};

    char *c;


    input(a,b);                                  //调用输入函数

    c = my_strcat(a,b);                          //调用连接函数


    printf("a+b=%s\n",c);


    return 0;

}

0 0
原创粉丝点击