Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //string_util.h
- //Libary providing string utilities
- #include <iostream>
- #include <string>
- #include <map>
- namespace CharlesLib{
- class string_util
- {
- public:
- //Converts a string number into a int(five -> 5)
- static int textToInt(std::string text);
- //remove all ocurrances of a string sequence
- static std::string removeAll(std::string words, std::string search);
- //remove all ocurrances of a string sequence after start position
- static std::string removeAll(std::string words, std::string search, unsigned int startPos);
- };
- }
- //string_util.cpp
- //For command line build:
- // string_util.cpp
- // compile with: cl /c /EHsc string_util.cpp
- // post-build command: lib string_util.obj
- #include "string_util.h"
- namespace CharlesLib {
- //map of hundreds
- std::map<std::string, unsigned int> hundreds{ {"one hundred", 100},{"two hundred", 200},{"three hundred", 300},{"four hundred", 400},{"five hundred",500},{"six hundred", 600},
- {"seven hundred",700},{"eight hundred", 800},{"nine hundred", 900} };
- //map of ones; also includes exceptions, namely: eleven and 12
- std::map<std::string, unsigned int> ones{ {"one", 1},{"two", 2},{"three", 3},{"four",4},{"five",5},{"six",6},{"seven",7},{"eight",8},{"nine",9},{"eleven",11},{"twelve",12} };
- //map of tens
- std::map<std::string, unsigned int> tens{ {"ten", 10},{"twenty",20},{"thirty",30},{"forty",40},{"fifty",50},{"sixty",60},{"seventy",70},{"eighty",80},{"ninty",90} };
- int CharlesLib::string_util::textToInt(std::string text) {
- int sum = 0;//store number as unt
- if (text.find("hundred") != std::string::npos) {//see if there is hundred
- for (auto const& [key, value] : hundreds) {
- if (text.find(key) != std::string::npos) {//find the hundred
- sum += value;
- std::string::size_type i = text.find(key);
- if (i != std::string::npos)
- {
- text.erase(i, key.length());//remove hundred
- }
- break;
- }
- }
- }
- for (auto const& [key, value] : tens) {
- if (text.find(key) != std::string::npos) {//find the 10s
- sum += value;
- std::string::size_type i = text.find(key);
- if (i != std::string::npos)
- {
- text.erase(i, key.length());//remove tens
- }
- break;
- }
- }
- for (auto const& [key, value] : ones) {
- if (text.find(key) != std::string::npos) {
- sum += value;
- std::string::size_type i = key.find(key);//find ones
- if (i != std::string::npos)
- {
- text.erase(i, text.length());//remove ones
- }
- break;
- }
- }
- return sum;
- }
- }
- std::string CharlesLib::string_util::removeAll(std::string words, std::string search) {
- unsigned int pos = 0;
- while (words.find(search) != std::string::npos) {
- pos = (unsigned int)words.find(search);
- words.replace(pos, search.length(), "");
- }
- return words;
- }
- std::string CharlesLib::string_util::removeAll(std::string words, std::string search, unsigned int startPos) {
- unsigned int pos = 0;
- while (words.find(search) != std::string::npos) {
- pos = (unsigned int)words.find(search);
- if (pos > startPos) {
- words.replace(pos, search.length(), "");
- }
- }
- return words;
- }
Advertisement
Add Comment
Please, Sign In to add comment