-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ31.cpp
More file actions
80 lines (58 loc) · 1.47 KB
/
Q31.cpp
File metadata and controls
80 lines (58 loc) · 1.47 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
71
72
73
74
75
76
77
78
79
80
//31. Write a program to repeat a string a specified number of times.Example: Input: ("hello", 3), Output: "hellohellohello".
#include <iostream>
using namespace std;
string repeatString(string str, int n) {
string result = "";
for (int i = 0; i < n; i++) {
result += str;
}
return result;
}
int main() {
string str = "hello";
int n;
cout<<"Enter the string to repeat: ";
cin>>str;
cout<<"Enter the number of times to repeat: ";
cin>>n;
cout << "Repeated string: " << repeatString(str, n) << endl;
return 0;
}
#include <iostream>
using namespace std;
string repeatStringOptimized(string str, int n) {
string result = "";
result = string(n, str[0]);
return result;
}
int main() {
string str;
int n;
cout << "Enter the string: ";
cin >> str;
cout << "Enter the number of repetitions: ";
cin >> n;
cout << "Repeated String: " << repeatStringOptimized(str, n) << endl;
return 0;
}
#include <iostream>
#include <sstream>
using namespace std;
string repeatTheString(const string& str, int n) {
stringstream ss;
for (int i = 0; i < n; ++i) {
ss << str;
}
return ss.str();
}
// data member and member function
int main() {
string str;
int n;
cout << "Enter the string: ";
cin >> str;
cout << "Enter the number of repetitions: ";
cin >> n;
cout << "Repeated String: " << repeatTheString(str, n) << endl;
return 0;
}