[Ask]C++ Combine 2 String Problem

Hi Guys, I want to ask something.
This is my code and I don't know why it doesn't run.

It can run at the first and second looping,
but when I used strcat to combine these two string(first + second) it didn't run

Please help me out here
Thx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main( void )
{
	char first[7] = "Edward";
	char second[8] = "Anthony";

	char combine[20];

	for(int a = 0; a < strlen(first); a++)
	{
		printf("%c\n", first[a]);
	
	}

	printf("\n\n");
	for(int a = 0; a < strlen(second); a++)
	{
		printf("%c\n", second[a]);
	
	}
	
	
	strcat(combine, first);
	strcat(combine, second);

	for(int a = 0; a < strlen(combine); a++)
	{
	
		printf("%c", combine[a]);
	
	
	}

	getchar();
	return 0;
}

strcat finds a \0 in the destinations string (combine) and copies the source (first or second) to that place. your combine is not initialized so it may not contain a \0 at all. you could initialize it with char combine[20] = {0};
Thx, it can be done.
The combine output is "EdwardAnthony"(without space), but how for "Edward Anthony"??
Last edited on
1
2
3
strcat(combine, first);
strcat(combine, " ");
strcat(combine, second);
Topic archived. No new replies allowed.