-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.cpp
86 lines (86 loc) · 2.61 KB
/
source.cpp
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
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
namespace stringX {
void replace_all(string& mainString, string stringToReplace, string stringToReplaceWith) {
for (int i = mainString.find(stringToReplace); i != string::npos; i = mainString.find(stringToReplace)) {
mainString.replace(i, stringToReplace.size(), stringToReplaceWith);
}
}
void replace(string& mainString, string stringToReplace, string stringToReplaceWith, int findNthOccurrence) { //if 4th param is null (0), will replace first instance of string
int inc = 0;
int nth = mainString.find(stringToReplace);
while (nth != string::npos) {
if (inc == findNthOccurrence) {
break;
}
else {
nth = mainString.find(stringToReplace, nth + stringToReplace.size());
inc++;
}
}
mainString.replace(nth, stringToReplace.size(), stringToReplaceWith);
}
void splitString(string mainString, vector<string>& vec, string stringToSplitWith) {
int fPos = 0;
int lPos = mainString.find(stringToSplitWith);
while (lPos != string::npos) {
vec.push_back(mainString.substr(fPos, lPos - fPos));
fPos = lPos + stringToSplitWith.size();
lPos = mainString.find(stringToSplitWith, fPos);
}
vec.push_back(mainString.substr(fPos));
}
int numofstr(string main, string numOf) {
int find = -1;
int j = 0;
while ((find = main.find(numOf, find + 1)) != string::npos) j++;
return j;
}
bool isnum(string mainString) {
return mainString.find_first_not_of("0123456789") == string::npos;
}
void removeNonInt(string& mainString) {
int pos = 0;
string tempMain = "";
while (pos != mainString.size()) {
if (isnum(string(1, mainString[pos]))) {
tempMain.append(string(1,mainString[pos]));
}
pos++;
}
mainString = tempMain;
}
void reverse(std::string & string) {
std::string ts;
for (int i = 0; i < string.size(); i++) {
ts += string[string.size() - 1 - i]; // size - 1 cuz size returns starting from 1 but index starts from 0 so ye
}
string = ts;
}
void encrypt(std::string& data) {
std::string td;
for (int i = 0; i < data.size(); i++) {
char fc = data[i];
int sc = (int)fc;
std::string ti = std::to_string(sc - 1);
int tioti = std::stoi(ti);
td += (char)tioti;
}
reverse(td);
data = td;
}
void decrypt(std::string& data) {
std::string td;
for (int i = 0; i < data.size(); i++) {
int fc = (int)data[i];
std::string ti = std::to_string(fc + 1);
int tioti = std::stoi(ti);
td += (char)tioti;
}
reverse(td);
data = td;
}
}