-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcatenate-two-string.c
60 lines (45 loc) · 1.08 KB
/
concatenate-two-string.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* Author: AKHILESH SANTOSHWAR */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int i = 0, j = 0, k = 0;
char *str1, *str2, *str3, buffer;
// Get First String
printf("Enter First String: ");
str1 = malloc(1);
while(buffer = getchar()){
if(buffer == '\n' || buffer == EOF){
break;
}else{
str1[i] = buffer;
i++;
str1 = realloc(str1, i+1);
}
}
// Get Second String
printf("\nEnter Second String: ");
str2 = malloc(1);
while(buffer = getchar()){
if(buffer == '\n' || buffer == EOF){
break;
}else{
str2[j] = buffer;
j++;
str2 = realloc(str2, i+1);
}
}
// Concatenate String
str3 = malloc(i+j+1);
strcat(str3, str1);
strcat(str3, str2);
printf("\nString[1]: %s", str1);
printf("\nString[2]: %s", str2);
printf("\nString Concatenate: %s", str3);
printf("\nLength: %d", i+j);
free(str1);
free(str2);
free(str3);
printf("\n");
return 0;
}