Saturday 25 May 2013

nstrcat.cpp (strcat with a variable sized argument list)


//No fail safe included in this version -- to be added later

#include <cstdarg>
#include <iostream>
using namespace std;

char* nstrcat(char* str, ...);
int main() {
  char* result;
char sample[50] = "Hello";
result = nstrcat(sample, " this", " is", " a", " test");
return 0;
}
char* nstrcat(char* str, ...) {
va_list args;
va_start(args, str);
char* temp;
int i = 0;
int n = 0;
while(str[i])
i++;    //moving to the end of the initial string
while(temp = va_arg(args, char*)) { //moving to the next argument each loop
for(n=0;temp[n];i++, n++) {
str[i] = temp[n];
}
}
va_end(args);
return str;
}

No comments:

Post a Comment