-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
230 lines (172 loc) · 5.22 KB
/
functions.cpp
File metadata and controls
230 lines (172 loc) · 5.22 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// Created by yuga shinde on 9/17/23.
//
#include <string>
#include <iostream>
#include <cmath>
void swap(int* ptr1, int* ptr2) {
//1. void swap(int*, int*): takes pointers to 2 integers and swaps them
int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
std::string everyNchar(const std::string& inputStr, int n) {
std::string result;
for (int i = 0; i < inputStr.length(); i += n) {
result += inputStr[i];
}
return result;
}
void duplicateString(const std::string& inputStr, int count) {
//2
if (count <= 0) {
// If the integer is 0 or negative, print nothing.
return;
}
for (int i = 0; i < count; ++i) {
std::cout << inputStr;
}
}
std::string reverseNchar(const std::string& inputStr, int n) {
std::string result;
for (int i = inputStr.length() - 1; i >= 0; i -= n) {
result += inputStr[i];
}
return result;
}
std::string divisibleString(const std::string& inputStr, int divisor1, int divisor2) {
std::string result;
for (int i = 0; i < inputStr.length(); i++) {
if (i % divisor1 == 0 || i % divisor2 == 0) {
result += inputStr[i];
}
}
return result;
}
void partialRotate(char arr[], int length, int rotation) {
if (length <= 1) {
return; // Nothing to rotate for single-element or empty array
}
rotation = rotation % length; // Handle rotation larger than array length
// Reverse the first part (rotation)
for (int i = 0; i < rotation / 2; i++) {
std::swap(arr[i], arr[rotation - i - 1]);
}
// Reverse the second part (length - rotation)
for (int i = 0; i < (length - rotation) / 2; i++) {
std::swap(arr[rotation + i], arr[length - i - 1]);
}
// Reverse the whole array
for (int i = 0; i < length / 2; i++) {
std::swap(arr[i], arr[length - i - 1]);
}
}
void printEvery3rd(const char arr[], int length) {
for (int i = 0; i < length; i += 3) {
std::cout << arr[i];
}
std::cout << std::endl;
}
std::string reverseSentence(const std::string& inputStr) {
std::string reversedStr;
int end = inputStr.length();
int start = end;
while (start >= 0) {
while (start > 0 && inputStr[start - 1] != ' ') {
start--;
}
reversedStr += inputStr.substr(start, end - start);
if (start > 0) {
reversedStr += ' ';
}
end = start - 1;
start = end;
}
reversedStr += '\n';
return reversedStr;
}
std::string sumWindow(const std::string& inputStr, const int window[], int size, int windowSize) {
std::string result;
for (int i = 0; i <= size - windowSize; i++) {
int sum = 0;
for (int j = 0; j < windowSize; j++) {
sum += window[j];
}
result += inputStr[sum];
}
return result;
}
std::string diagonalString(const std::string& inputStr) {
int n = inputStr.length();
int m = static_cast<int>(std::sqrt(n));
// Ensure m is within bounds (m x m matrix)
if (m * m > n) {
m--;
}
std::string result;
for (int i = 0; i < m; i++) {
// Append the diagonal characters
result += inputStr[i * (m + 1)];
}
return result;
}
std::string reverseDiagonalString(const std::string& inputStr) {
int n = inputStr.length();
int m = static_cast<int>(std::sqrt(n));
// Ensure m is within bounds (m x m matrix)
if (m * m > n) {
m--;
}
std::string result;
for (int i = 0; i < m; i++) {
// Append the reverse diagonal characters
result += inputStr[i * (m + 1) + m];
}
return result;
}
std::string transposeString(const std::string& inputStr) {
int n = inputStr.length();
int m = static_cast<int>(std::sqrt(n));
// Ensure m is within bounds (m x m matrix)
if (m * m > n) {
m--;
}
std::string result;
for (int col = 0; col < m; col++) {
for (int row = 0; row < m; row++) {
// Calculate the index in the original string
int index = row * m + col;
result += inputStr[index];
}
}
return result;
}
bool checkInString(const std::string& inputStr, char targetChar, int currentIndex) {
// Base case: If currentIndex is out of bounds, return false.
if (currentIndex < 0 || currentIndex >= inputStr.length()) {
return false;
}
// Check if the character at currentIndex matches the target character.
if (inputStr[currentIndex] == targetChar) {
return true;
}
// Recursive case: Move to the next index and make a recursive call.
return checkInString(inputStr, targetChar, currentIndex + 1);
}
void multiCheckInString(const std::string& str1, const std::string& str2, int currentIndex) {
if (currentIndex < 0 || currentIndex >= str1.length()){
return;
}
char currentChar = str1[currentIndex];
if (!checkInString(str2, currentChar, 0)) {
// If it's not in the second string, print it.
std::cout << ">" << currentChar << std::endl;
}
multiCheckInString(str1, str2, currentIndex + 1);
}
int main() {
std::string str1 = "acokpdae";
std::string str2 = "vtmipksa";
multiCheckInString(str1, str2, 0);
return 0;
}