-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
64 lines (56 loc) · 1.36 KB
/
utils.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
/**
*
* Solution to course project # 4
* Introduction to programming course
* Faculty of Mathematics and Informatics of Sofia University
* Winter semester 2021/2022
*
* @author Tihomir Galov
* @idnumber 1MI0600118
* @compiler GCC
*
* <file with helper functions for working with strings>
*
*/
#include <string>
#include "utils.h"
std::string splitByIndex(std::string &data, int index) {
int len = data.length();
int comma_counter = 0;
std::string output;
for (int i = 0; i < len; i++) {
if (data[i] == ',') {
comma_counter++;
continue;
}
if (comma_counter == index)
output.push_back(data[i]);
else if (comma_counter > index)
break;
}
return output;
}
void strip(std::string &s) {
if (s[0] == ' ' && s[s.length() - 1] == ' ') {
s.erase(0, 1);
s.erase(s.length() - 1, 1);
}
}
int isInArray(std::string names[], const std::string &substr) {
unsigned int len = names->length();
for (int i = 0; i < len; i++) {
if (names[i] == substr)
return i;
}
return -1;
}
bool isOpeningBracelet(char a) {
return a == '{';
}
bool isClosingBracelet(char a) {
return a == '}';
}
bool isLoopIn(std::string &s, size_t index) {
return (s[index] == 'i' && s[index + 1] == 'n' && s[index - 1] == ' ' &&
s[index + 2] == ' ');
}