Advertisement
TermSpar

C++ Custom String Methods

Dec 3rd, 2019
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.82 KB | None | 0 0
  1. #include "StringMethods.h"
  2.  
  3. StringMethods::StringMethods() {
  4. }
  5.  
  6. /********** toUpper **********/
  7.  
  8. // convert string to uppercase:
  9. std::string StringMethods::toUpper(std::string str) {
  10.     std::string retString = "";
  11.     for (int i = 0; i < str.length(); i++) {
  12.         // if the letter is lowercase:
  13.         if (str.at(i) >= 97 && str.at(i) <= 122) {
  14.             // make it uppercase:
  15.             retString += (str.at(i) - ASCII_LETTER_CONSTANT);
  16.         }
  17.         else {
  18.             // otherwise just leave it:
  19.             retString += str.at(i);
  20.         }
  21.     }
  22.     return retString;
  23. }
  24.  
  25. /********** toLower **********/
  26.  
  27. // convert string to lowercase:
  28. std::string StringMethods::toLower(std::string str) {
  29.     std::string retString = "";
  30.     for (int i = 0; i < str.length(); i++) {
  31.         // if the letter is uppercase:
  32.         if (str.at(i) >= 65 && str.at(i) <= 90) {
  33.             // make it lowercase:
  34.             retString += (str.at(i) + ASCII_LETTER_CONSTANT);
  35.         }
  36.         else {
  37.             // otherwise just leave it:
  38.             retString += str.at(i);
  39.         }
  40.     }
  41.     return retString;
  42. }
  43.  
  44. /********** toCamel **********/
  45.  
  46. // convert string to camelcase:
  47. std::string StringMethods::toCamel(std::string str) {
  48.     std::string retString = "";
  49.     for (int i = 0; i < str.length(); i++) {
  50.         // if the char is uppercase
  51.         if ((str.at(i) >= 65 && str.at(i) <= 90)) {
  52.             // if it's first or right after a space:
  53.             if (i == 0 || str.at(i - 1) == ' ') {
  54.                 // leave it:
  55.                 retString += str.at(i);
  56.             }
  57.             else {
  58.                 // otherwise make it lowercase:
  59.                 retString += (str.at(i) + ASCII_LETTER_CONSTANT);
  60.             }
  61.         }
  62.         // if the char is lowercase
  63.         else if ((str.at(i) >= 97 && str.at(i) <= 122)) {
  64.             // if it's first or right after a space:
  65.             if (i == 0 || str.at(i - 1) == ' ') {
  66.                 // make it uppercase:
  67.                 retString += (str.at(i) - ASCII_LETTER_CONSTANT);
  68.             }
  69.             else {
  70.                 // otherwise leave it:
  71.                 retString += (str.at(i));
  72.             }
  73.         }
  74.         else {
  75.             // if not a letter, then leave it:
  76.             retString += str.at(i);
  77.         }
  78.     }
  79.     return retString;
  80. }
  81.  
  82. /********** toString **********/
  83.  
  84. // convert integer to string:
  85. std::string StringMethods::toString(int num) {
  86.     std::list<int> digits;
  87.     std::string finalString = "";
  88.  
  89.     // if the number is zero, leave it:
  90.     if (num == 0) {
  91.         digits.push_back(0);
  92.     }
  93.     // otherwise:
  94.     else {
  95.         // loop through every digit:
  96.         while (num != 0) {
  97.             // isolate it and put it in digits list:
  98.             int last = num % 10;
  99.             digits.push_front(last);
  100.             num = (num - last) / 10;
  101.         }
  102.     }
  103.     // loop digits list:
  104.     for (std::list<int>::iterator it = digits.begin(); it != digits.end(); ++it) {
  105.         // convert each digit to ascii by adding 48, and add it to finalString:
  106.         finalString += (*it + ASCII_NUM_CONSTANT);
  107.     }
  108.     return finalString;
  109. }
  110.  
  111. /********** toInt **********/
  112.  
  113. // convert string to integer:
  114. int StringMethods::toInt(std::string str) {
  115.     std::vector<int> intVec;
  116.     int finalInt = 0;
  117.     int digitPlaces = str.length();
  118.  
  119.     // get each digit as an int into intVec:
  120.     for (int i = digitPlaces - 1; i >= 0; i--) {
  121.         intVec.push_back(str.at(i) - ASCII_NUM_CONSTANT);
  122.     }
  123.  
  124.     // combine digits:
  125.     int place = 1;
  126.     for (int i = 0; i < digitPlaces; i++) {
  127.         // if the digit is a zero, just mult places by 10:
  128.         if (intVec.at(i) == 0) {
  129.             place *= 10;
  130.         }
  131.         // otherwise mult the current num by its place, and add it to finalNum:
  132.         else {
  133.             finalInt += intVec.at(i) * place;
  134.             place *= 10;
  135.         }
  136.     }
  137.     return finalInt;
  138. }
  139.  
  140. /********** split **********/
  141.  
  142. // split a string on a certain char:
  143. std::vector<std::string> StringMethods::split(std::string str, char breakOn) {
  144.     std::vector<std::string> sVec;
  145.     std::string statement = "";
  146.     str += breakOn;
  147.     // traverse str:
  148.     for (unsigned int i = 0; i < str.size(); i++) {
  149.         // if iteration != breakOn:
  150.         if (str.at(i) != breakOn) {
  151.             // add it to statement:
  152.             statement += str.at(i);
  153.         }
  154.         // if iteration == breakOn:
  155.         else if (str.at(i) == breakOn) {
  156.             // push vector with statement
  157.             statement += str.at(i);
  158.             sVec.push_back(statement);
  159.             // reset statement:
  160.             statement = "";
  161.         }
  162.     }
  163.     // remove appended char
  164.     statement += sVec.at(1);
  165.     statement = statement.substr(0, statement.length() - 1);
  166.     sVec.at(1) = statement;
  167.  
  168.     return sVec;
  169. }
  170.  
  171. /********** concat **********/
  172.  
  173. // combine two strings:
  174. std::string StringMethods::concat(std::string str1, std::string str2) {
  175.     std::string retString;
  176.     retString = str1 + str2;
  177.     return retString;
  178. }
  179.  
  180. /********** substring **********/
  181.  
  182. // get string from one index to another:
  183. std::string StringMethods::substring(std::string str, int start, int end) {
  184.     std::string retString;
  185.     // copy all chars from specified start to specified end into retString:
  186.     for (int i = start; i < end; i++) {
  187.         retString += str.at(i);
  188.     }
  189.     return retString;
  190. }
  191.  
  192. /********** trim **********/
  193.  
  194. // trim whitespace off the front and back of a string:
  195. std::string StringMethods::trim(std::string str) {
  196.     std::string retString = str;
  197.     // if the first char is a space:
  198.     if (str.at(0) == ' ') {
  199.         // delete it:
  200.         retString = substring(str, 1, str.length() - 1);
  201.         str = retString;
  202.     }
  203.     // if the last char is a space:
  204.     if (str.at(str.length() - 1) == ' ') {
  205.         // delete it:
  206.         retString = substring(str, 0, str.length() - 1);
  207.     }
  208.     return retString;
  209. }
  210.  
  211. /********** contains **********/
  212.  
  213. // check if one string contains another:
  214. bool StringMethods::contains(std::string str, std::string check) {
  215.     for (int i = 0; i < str.length(); i++) {
  216.         // if current char in str is equal to the first char in check:
  217.         if (str.at(i) == check.at(0)) {
  218.             // see if the rest of the characters match:
  219.             int tempCount = i;
  220.             for (int j = 0; j < check.length(); j++) {
  221.                 // if the current str letter does not match check's letter:
  222.                 if (!(str.at(tempCount) == check.at(j))) {
  223.                     // exit the inner loop:
  224.                     goto breakOut;
  225.                 }
  226.                 // otherwise:
  227.                 else {
  228.                     // if you get to the last letter:
  229.                     if (j == (check.length() - 1)) {
  230.                         // then the word is there:
  231.                         return true;
  232.                     }
  233.                 }
  234.                 tempCount++;
  235.             }
  236.         }
  237.     breakOut:
  238.         std::cout << "";
  239.     }
  240.     // otherwise, the word is not there:
  241.     return false;
  242. }
  243.  
  244. // check if a string contains a char:
  245. bool StringMethods::contains(std::string str, char check) {
  246.     for (int i = 0; i < str.length(); i++) {
  247.         if (str.at(i) == check) {
  248.             return true;
  249.         }
  250.     }
  251.     return false;
  252. }
  253.  
  254. /********** isEqual **********/
  255.  
  256. // check if two strings are equal (have the same sequence of chars):
  257. bool StringMethods::isEqual(std::string str1, std::string str2) {
  258.     // if the strings are the same length:
  259.     if (str1.length() == str2.length()) {
  260.         for (int i = 0; i < str1.length(); i++) {
  261.             // if a character is not the same:
  262.             if (!(str1.at(i) == str2.at(i))) {
  263.                 return false;
  264.             }
  265.             else {
  266.                 // if you get to the last char:
  267.                 if (i == (str1.length() - 1)) {
  268.                     return true;
  269.                 }
  270.             }
  271.         }
  272.     }
  273.     // if the strings have different lengths:
  274.     return false;
  275. }
  276.  
  277. StringMethods::~StringMethods() {
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement