Thursday, 29 November 2012

Week13_StrCat function - Concatenate optional strings

#include <iostream>
#include <cstdarg>
#include <string.h>
using namespace std;

char* StrCat(char* des, char* src1, ...);
int main() {
    char str[500] = "Fred";
    StrCat(str, "Soly", "Moly", 0);
    cout << str << endl;
    return 0;
}

char* StrCat(char* des,char* src1, ...) {
    strcat(des, src1);
    va_list varg;
    va_start(varg, src1);
    char* i =va_arg(varg, char*);
    while(i!=0) {
        strcat(des, i);
        i = va_arg(varg, char*);
    }
    va_end(varg);
    return des;
}

output is :
FredSolyMoly

1 comment:

  1. Its ironic to use strcat in a strcat function.

    I took your version and made some adjustments:
    http://peter.ug/strcat-function/

    ReplyDelete