• 最熟悉的就是strcpy/stncpy
  • 其次就是memcpy
  • 然后还有strcat/strncat
  • 最后一种强大的就是自行暴力咯。
  • 下面给一段包含所有的代码:
#include <stdio.h>

#include <stdarg.h>

/*

 *  sunus lee

 *  Mar 10, 2012

 *  call this function in this format: 

 *  p = strjoin(buf, "^^", "string1", "string2", "string3", NULL);

 *  the parameters MUST END WITH NULL.

 *  No safety check for this function, buf MUST BE BIG ENOUGH!

 */

/*

 *  update**

 *  sunus lee

 *  Mar 15, 2012

 *  you do NOT need to manually add the last parameters(NULL)

 *  anymore, i wrote a warpper to do this for you already!

 *  now just call strjoin(buf, "^^", "str1", "str2", "str3");

 */

#define strjoin(...)    _strjoin(__VA_ARGS__, NULL)

char *_strjoin(char *buf, char *delim, ...)

{

    char *p, *res, *d;

    int i = 0;

    va_list ap;

    va_start(ap, delim);

    res = buf;

    p = va_arg(ap, char *);

    while(p)

    {

        while(*res++ = *p++) 

            /* do nothing */;

        res--;

        if(p = va_arg(ap, char *))

        {

            d = delim;

            while(*res++ = *d++) 

                /* do nothing */;

            res--;

        }

    }

    *res = \'\\0\';

    va_end(ap);

    return buf;

}

int main()

{

    char buf[200] = "i am buf";

    char *p;

p = strjoin(buf, “^_^”, buf, “i am sunus”, “vivian is me”, “i play counter-strike.”);

    printf("p is :\\n%s", p);

    p = strjoin(buf, "^_^", "first line", "i am sunus", "vivian is me", "i play counter-strike.");

    printf("p is :\\n%s", p);

    return 0;

}

其中 p = strjoin(buf, "^^", "string1", "string2", "string3", NULL)中的buf必须足够大。

1 对 “常见的C语言中连接两个字符串的方法[转]”的想法;

评论被关闭。