[英]merging of text
Can you please help me with merging of two texts into one using just only stdio.h
and stdlib.h
? 您能帮我仅使用
stdio.h
和stdlib.h
将两个文本合并为一个吗? The result should be HelloWorld
. 结果应该是
HelloWorld
。
So far, I have the following, but there is a mistake somewhere. 到目前为止,我有以下内容,但是某处有一个错误。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
char *spojeni(char *t1, char *t2)
{
char pole_spolecne[10];
for (*t1 = 0; *t1 < 5; t1++)
{
pole_spolecne[*t1] = *t1;
}
for (*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, but it returns an error at the 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, this change of function helped: char *spojeni(char *t1, char *t2) 最后,这种功能上的改变有助于: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 am not quite sure how to answer this, as this is clearly a teaching exercise, and, to be blunt, the code given shows a lack of understanding of pointers. 我不太确定如何回答这个问题,因为这显然是一种教学活动,老实说,给出的代码表明对指针缺乏理解。 And pointers is a topic better taught in person than via a web-site comment.
指针是一个比通过网站评论更好地亲自授课的话题。
A few hints, though: 一些提示,但是:
Think very clearly about pointers, what they are pointing to, and what makes them different from array indices. 非常清楚地考虑一下指针,它们指向的对象以及它们与数组索引的不同之处。
Draw diagrams to visualize what you're doing. 绘制图表以可视化您在做什么。
Your exercise can be solved using calloc(), strlen() and strcpy(). 您可以使用calloc(),strlen()和strcpy()解决您的运动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.