-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringOperations.cpp
93 lines (74 loc) · 1.82 KB
/
StringOperations.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
87
88
89
90
91
92
93
// GAGS
// Developed by Andrew R Wood
#include <string>
#include <sstream>
#include <vector>
#include <functional>
#include <iostream>
#include "StringOperations.h"
using namespace std;
// function to convert string to double
double stringToDouble(string s) {
double d;
stringstream ss(s); //turn the string into a stream
ss >> d; //convert
return d;
}
// function to convert string to int
int stringToInt(string s) {
int i;
stringstream ss(s); //turn the string into a stream
ss >> i; //convert
return i;
}
// function to convert int to string
string intToString(int i) {
string s;
ostringstream convert;
convert << i;
s = convert.str();
return s;
}
// function to convert double to string
string doubleToString(double d) {
string s;
ostringstream convert;
convert << d;
s = convert.str();
return s;
}
// function to split a string based on a delimiter
//
//void split(string& s, char c, vector<string>& v) {
void split(string& s, string sc, vector<string>& v) {
string::size_type i = 0;
string::size_type j = s.find(sc);
while (j != string::npos) {
v.push_back(s.substr(i,j-i));
i = ++j;
j = s.find(sc,j);
if (j == string::npos) {
v.push_back(s.substr(i, s.length()));
}
}
if (v.size() == 0) {
v.push_back(s);
}
}
/*
// function to split a string based on a delimiter
template<typename T>
void split(const basic_string<T>& s, T c,
vector<basic_string<T> >& v) {
basic_string<T>::size_type i = 0;
basic_string<T>::size_type j = s.find(c);
while (j != basic_string<T>::npos) {
v.push_back(s.substr(i, j-i));
i = ++j;
j = s.find(c, j);
if (j == basic_string<T>::npos)
v.push_back(s.substr(i, s.length()));
}
}
*/