-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringProcessing.cpp
More file actions
74 lines (69 loc) · 1.64 KB
/
stringProcessing.cpp
File metadata and controls
74 lines (69 loc) · 1.64 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
/*
* stringProcessing.cpp
* Krish Savla and Ranvir Malik
* 24 April, 2022
*
* Comp 15 Project 3 - gerp
*
* Implementation of the stringProcessing class. Defines the only member
* function of the class.
*/
#include <string>
#include "stringProcessing.h"
#include <iostream>
using namespace std;
/*
Name: stripNonAlphaNum
Parameters: a string
Return value: a string
Purpose: remove any special characters from a string
Other information: n/a
*/
std::string stripNonAlphaNum(std::string s) {
size_t i = 0, start = 0, end = 0;
int counter = 0; // counts number of numbers or letters
// gets the starting index
while (i < s.length()) {
// ascii values for digits, letters, or capital letters
if (s[i] >= 48 and s[i] <= 57) {
start = i;
i = s.length(); // ends the loop
counter++;
}
else if (s[i] >= 65 and s[i] <= 90) {
start = i;
i = s.length();
counter++;
}
else if (s[i] >= 97 and s[i] <= 122) {
start = i;
i = s.length();
counter++;
}
else
i++;
}
i = 0;
// gets the ending index
while (i < s.length()) {
if ((s[i] >= 48 and s[i] <= 57)) {
end = i;
}
else if (s[i] >= 65 and s[i] <= 90) {
end = i;
}
else if (s[i] >= 97 and s[i] <= 122) {
end = i;
}
i++;
}
// if only special characters
if (counter == 0) {
return "";
}
string s1 = "";
for (size_t j = start; j <= end; j++) {
s1 = s1 + s[j];
}
return s1;
}