c - merging of text -
can please me merging of 2 texts 1 using stdio.h
, stdlib.h
? result should helloworld
.
so far, have following, there mistake somewhere.
#define _crt_secure_no_warnings #include <stdio.h> #include <stdlib.h> char *spojeni(char *t1, char *t2) { char pole_spolecne[10]; (*t1 = 0; *t1 < 5; t1++) { pole_spolecne[*t1] = *t1; } (*t2 = 0; *t2 < 10; t2++) { pole_spolecne[*t2 + 5] = *t2; } return pole_spolecne; } int main() { char pole1[] = { "hello" }; char pole2[] = { "world" }; printf("%s\n", spojeni(pole1, pole2)); system("pause"); return 0; }
my new solution, returns error @ end:
#define _crt_secure_no_warnings #include <stdio.h> #include <stdlib.h> char *spojeni(char *t1, char *t2) { char pole_cele[20]; char *p_pole_cele; p_pole_cele = t1; strcat(p_pole_cele, t2); return p_pole_cele; } int main() { char pole1[] = { "hello" }; char pole2[] = { "world" }; char *p_pole1; char *p_pole2; p_pole1 = pole1; p_pole2 = pole2; printf("%s\n)", spojeni(p_pole1, p_pole2)); system("pause"); return 0; }
finally, change of function helped: char *spojeni(char *t1, char *t2)
{ char pole_cele[20]; char *p_pole_cele; p_pole_cele = (char *)malloc(10); strcpy(p_pole_cele, t1); p_pole_cele = (char *)realloc(p_pole_cele, 20); strcat(p_pole_cele, t2); return p_pole_cele; }
i not quite sure how answer this, teaching exercise, and, blunt, code given shows lack of understanding of pointers. , pointers topic better taught in person via web-site comment.
a few hints, though:
think pointers, pointing to, , makes them different array indices.
draw diagrams visualize you're doing.
your exercise can solved using calloc(), strlen() , strcpy().
Comments
Post a Comment