forked from gutty333/Medium-Programming-Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
26_NumberEncoding.cpp
49 lines (43 loc) · 1.57 KB
/
26_NumberEncoding.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
// For this challenge you will encode a given string following a specific rule.
// have the function NumberEncoding(str) take the str parameter and encode the message according to the following rule: encode every letter into its corresponding numbered position in the alphabet. Symbols and spaces will also be used in the input. For example: if str is "af5c a#!" then your program should return 1653 1#!.
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>
using namespace std;
string NumberEncoding(string str) {
// Making all the characters in the string lower case
// This will make it easier to determine alphabet position calculation later on.
for (int x = 0; x < str.length(); x++)
{
str[x] = tolower(str[x]);
}
string temp;
for (int y = 0; y < str.length(); y++)
{
// If the character is a letter than find the position
if (str[y] >= 'a' && str[y] <= 'z')
{
// Calculation to find the position
// Since we are dealing with lowercase only, this is simple to do with ascii value
// To handle double digits we would need to take the result as an int than store as a string datatype
int position = int(str[y]) - 96;
stringstream convert;
convert << position;
temp += convert.str();
}
else
{
// If we find symbols or other numbers just do it, JUST DO IT
temp.push_back(str[y]);
}
}
return temp;
}
int main() {
// keep this function call here
cout << NumberEncoding("hello 45") << endl; // 85121215 45
cout << NumberEncoding("jaj-a") << endl; // 10110-1
cout << NumberEncoding("af5c a#!") << endl; // 1653 1#!
return 0;
}