Created
May 11, 2018 17:10
-
-
Save edenizk/26381aed01700fd59e11b3cd2b58307b to your computer and use it in GitHub Desktop.
Dynamic Strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
unsigned scat(char *s, char *s2) | |
{ | |
int i,j; | |
for(i=0;s[i]!=0; i++); | |
s[i++]=32; | |
for(j=0;s2[j]!=0;j++){ | |
s[i]=s2[j]; | |
i++; | |
} | |
s[i]=0; | |
} | |
unsigned slen (char *s) | |
{ | |
int i; | |
for(i=0;s[i]!=0;i++); | |
return i; | |
} | |
char* create(char *s,char *s2) | |
{ | |
char* s3; | |
s3=malloc(slen(s)+slen(s2)+1); | |
//assent(s3!=NULL); | |
scat(s3,s); | |
scat(s3,s2); | |
printf(" "); | |
return s3; | |
} | |
int main() | |
{ | |
char str1[50]="Alice"; | |
char str2[]="dog"; | |
char str3[]="Peter",str4[]="Cat"; | |
char *st1,*st2; | |
st1=create(str1,str2); | |
printf("st1=%s\nst1=%p\nst2=%s\nst2=%p\n\n",st1,st1,st2,st2);\ | |
//free(st1); | |
st2=create(str3,str4); | |
//free(st2); | |
printf("st2=%s\nst2=%p\nst1=%s\nst1=%p\n\n",st2,st2,st1,st1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment