-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ23.cpp
More file actions
70 lines (54 loc) · 1.44 KB
/
Q23.cpp
File metadata and controls
70 lines (54 loc) · 1.44 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
// 23. Write a program to concatenate two given strings. Example: Input: ("hello", " world"), Output: "hello world".
#include <iostream>
using namespace std;
string concatenateManual(string str1, string str2) {
for (int i = 0; i < str2.length(); i++) {
str1 += str2[i];
}
return str1;
}
int main() {
string str1 = "hello";
string str2 = " world";
cout << "Concatenated String (Brute Force): " << concatenateManual(str1, str2) << endl;
return 0;
}
//
#include <iostream>
using namespace std;
string concatenateWithPlus(string str1, string str2) {
return str1 + str2;
}
int main() {
string str1 = "hello";
string str2 = " world";
cout << "Concatenated String (Using +): " << concatenateWithPlus(str1, str2) << endl;
return 0;
}
//
#include <iostream>
using namespace std;
string concatenateWithAppend(string str1, string str2) {
str1.append(str2);
return str1;
}
int main() {
string str1 = "hello";
string str2 = " world";
cout << "Concatenated String (Using append()): " << concatenateWithAppend(str1, str2) << endl;
return 0;
}
//
#include <iostream>
#include <cstring>
using namespace std;
void concatenateWithStrcat(char* str1, const char* str2) {
strcat(str1, str2);
}
int main() {
char str1[50] = "hello";
const char* str2 = " world";
concatenateWithStrcat(str1, str2);
cout << "Concatenated String (Using strcat()): " << str1 << endl;
return 0;
}