Skip to content

Instantly share code, notes, and snippets.

@joeke80215
Created February 16, 2019 06:58
Show Gist options
  • Save joeke80215/eeafd5a4642964cf37235e2d309fa7c5 to your computer and use it in GitHub Desktop.
Save joeke80215/eeafd5a4642964cf37235e2d309fa7c5 to your computer and use it in GitHub Desktop.
Combine New String with C
#include <stdio.h>
#include <stdlib.h>
char* CombineNewString(char *w1,char *w2)
{
size_t count1,count2;
int index;
char *t,*t1,*t2;
count1 = 0;
count2 = 0;
index = 0;
t1 = w1;
t2 = w2;
while(*t1){
count1++;
t1++;
}
while(*t2){
count2++;
t2++;
}
t = (char*)malloc(sizeof(char) * (count1 + count2));
for(size_t i = 0; i < count1; i++)
{
*(t + index) = *(w1 + i);
index++;
}
for(size_t i = 0; i < count2; i++)
{
*(t + index) = *(w2 + i);
index++;
}
*(t + index) = '\0';
return t;
}
/*
output:
abcdef
*/
void main(void)
{
char w1[] = "abc";
char w2[] = "def";
char *tt;
tt = CombineNewString(w1,w2);
printf("%s\n",tt);
getchar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment