Repository navigation
Expand file tree
/
Copy pathnetwork.cpp
More file actions
58 lines (53 loc) · 1.76 KB
/
Copy pathnetwork.cpp
File metadata and controls
58 lines (53 loc) · 1.76 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
#include "mpesa.h"
#include <unordered_map>
#include <string>
using namespace std;
const unordered_map<string, string> networkMap = {
{"071", "MIC Tanzania PLC"},
{"065", "MIC Tanzania PLC"},
{"067", "MIC Tanzania PLC"},
{"073", "Tanzania Telecommunications Corporation"},
{"074", "Vodacom Tanzania PLC"},
{"075", "Vodacom Tanzania PLC"},
{"076", "Vodacom Tanzania PLC"},
{"077", "Zanzibar Telecom PLC"},
{"078", "Airtel Tanzania PLC"},
{"068", "Airtel Tanzania PLC"},
{"069", "Airtel Tanzania PLC"},
{"066", "Smile Communications Tanzania Limited"},
{"061", "Viettel Tanzania PLC"},
{"062", "Viettel Tanzania PLC"},
{"063", "Mkulima African Telecommunication Company Limited"},
{"064", "Wiafrica Tanzania Limited"},
{"072", "MO Mobile Holding Limited"}
};
string formatPhoneNumber(const string &input) {
// "255XXYYYYYY" -> "0XX"
if (input.length() >= 5 && input.substr(0, 3) == "255") {
return "0" + input.substr(3, 2);
}
// "+255XXYYYYYY" -> "0XX"
if (input.length() >= 6 && input[0] == '+' && input.substr(1, 3) == "255") {
return "0" + input.substr(4, 2);
}
// "0XXYYYYYY" -> "0XX"
if (input.length() >= 3 && input[0] == '0') {
return input.substr(0, 3);
}
// Bare 2-digit network code -> prefix "0"
if (input.length() >= 2) {
return "0" + input.substr(0, 2);
}
return "";
}
string getNetwork(const string &phoneNumber) {
if (phoneNumber.length() < 3) {
return "Invalid Number";
}
string networkCode = formatPhoneNumber(phoneNumber);
if (networkCode.empty()) {
return "Invalid Number";
}
auto it = networkMap.find(networkCode);
return (it != networkMap.end()) ? it->second : "Unknown Network";
}