Skip to content

Commit 9712df4

Browse files
committed
null terminated strings
1 parent fd6efe6 commit 9712df4

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

prog.10.3.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
3+
int main (void)
4+
{
5+
void concat (char result[], const char str1[], const char str2[]);
6+
const char s1[] = { "Test " };
7+
const char s2[] = { "works." };
8+
char s3[20];
9+
10+
concat (s3, s1, s2);
11+
12+
printf ("%s\n", s3);
13+
14+
return 0;
15+
}
16+
17+
// Function to concatenate two character strings
18+
19+
void concat (char result[], const char str1[], const char str2[])
20+
{
21+
int i, j;
22+
23+
// copy str1 to result
24+
25+
for ( i = 0; str1[i] != '\0'; ++i )
26+
result[i] = str1[i];
27+
28+
// copy str2 to result
29+
30+
for ( j = 0; str2[j] != '\0'; ++j )
31+
result[i + j] = str2[j];
32+
33+
// Terminate the concated string with a null character
34+
35+
result [i + j] = '\0';
36+
}

0 commit comments

Comments
 (0)