Guest User

Untitled

a guest
Aug 7th, 2021
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1.  
  2.  
  3. //string_util.h
  4. //Libary providing string utilities
  5. #include <iostream>
  6. #include <string>
  7. #include <map>
  8.  
  9. namespace CharlesLib{
  10.  
  11.     class string_util
  12.     {
  13.     public:
  14.         //Converts a string number into a int(five -> 5)
  15.         static int textToInt(std::string text);
  16.  
  17.         //remove all ocurrances of a string sequence
  18.         static std::string removeAll(std::string words, std::string search);
  19.  
  20.         //remove all ocurrances of a string sequence after start position
  21.         static std::string removeAll(std::string words, std::string search, unsigned int startPos);
  22.  
  23.     };
  24. }
  25.  
  26.  
  27. //string_util.cpp
  28.  
  29. //For command line build:
  30. // string_util.cpp
  31. // compile with: cl /c /EHsc string_util.cpp
  32. // post-build command: lib string_util.obj
  33.  
  34. #include "string_util.h"
  35. namespace CharlesLib {
  36.  
  37.     //map of hundreds
  38.     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},
  39.         {"seven hundred",700},{"eight hundred", 800},{"nine hundred", 900} };
  40.  
  41.     //map of ones; also includes exceptions, namely: eleven and 12
  42.     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} };
  43.  
  44.     //map of tens
  45.     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} };
  46.  
  47.     int CharlesLib::string_util::textToInt(std::string text) {
  48.         int sum = 0;//store number as unt
  49.         if (text.find("hundred") != std::string::npos) {//see if there is hundred
  50.             for (auto const& [key, value] : hundreds) {
  51.                 if (text.find(key) != std::string::npos) {//find the hundred
  52.                     sum += value;
  53.                     std::string::size_type i = text.find(key);
  54.  
  55.                     if (i != std::string::npos)
  56.                     {
  57.                         text.erase(i, key.length());//remove hundred
  58.                     }
  59.                     break;
  60.                 }
  61.             }
  62.         }
  63.  
  64.         for (auto const& [key, value] : tens) {
  65.             if (text.find(key) != std::string::npos) {//find the 10s
  66.                 sum += value;
  67.                 std::string::size_type i = text.find(key);
  68.  
  69.                 if (i != std::string::npos)
  70.                 {
  71.                     text.erase(i, key.length());//remove tens
  72.                 }
  73.                 break;
  74.             }
  75.         }
  76.  
  77.         for (auto const& [key, value] : ones) {
  78.             if (text.find(key) != std::string::npos) {
  79.                 sum += value;
  80.                 std::string::size_type i = key.find(key);//find ones
  81.  
  82.                 if (i != std::string::npos)
  83.                 {
  84.                     text.erase(i, text.length());//remove ones
  85.                 }
  86.                 break;
  87.             }
  88.         }
  89.     return sum;
  90.     }
  91. }
  92.  
  93. std::string  CharlesLib::string_util::removeAll(std::string words, std::string search) {
  94.     unsigned int pos = 0;
  95.  
  96.     while (words.find(search) != std::string::npos) {
  97.         pos = (unsigned int)words.find(search);
  98.  
  99.         words.replace(pos, search.length(), "");
  100.     }
  101.     return words;
  102. }
  103.  
  104. std::string CharlesLib::string_util::removeAll(std::string words, std::string search, unsigned int startPos) {
  105.     unsigned int pos = 0;
  106.  
  107.     while (words.find(search) != std::string::npos) {
  108.         pos = (unsigned int)words.find(search);
  109.         if (pos > startPos) {
  110.             words.replace(pos, search.length(), "");
  111.         }
  112.     }
  113.     return words;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment