const char* nstrcat( char* to, …); //strcat with >= 1 arguments

Posted: May 31, 2013 in Programming
Tags: , , , , , , , , , , , ,

This program concatenates n amounts of strings, so in the output the user will see one long string.

https://github.com/admix/admixSoft/blob/master/OOP344/nstrcat.cpp

#include 
#include 
using namespace std;const char* nstrcat(char* to, ...);
int main(){  //Sample main() to test the program
    char str[100] = "";
    char first[] = "Space";
    char second[] = "Explorer";
    cout<<nstrcat(str, first, " ", second, " HeeHaw", '')<<endl;
    return 0;
}
const char* nstrcat( char* to, ...){
  char* inTo;
  int i = 0;
  va_list varg;
  va_start(varg, to);
  inTo = va_arg(varg, char*);
  while(*to)to++;
  while(inTo && *inTo){	//returns '1' if true
  	while(*inTo){
  		to[i] = *inTo;
  		i++;         //keep going through the arguments
  		inTo++;
  	}
  	inTo = va_arg(varg, char*);
  }
  va_end(varg);
  return to;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s