Search code examples
c++stdvectorstdstring

Save integers from string to vector


I need to save integers from string to vector.

Definition of number: each of strings substrings which consists entirely of digits, with a space before the first character of that substring and a space or punctuation mark after the last character, unless the substring is at the very beginning or end of the string (in this case there does not have to be a space in front or space or punctuation behind).

EXAMPLE 1: "120 brave students, 35 failed, remaining 85..." Numbers: 120, 35, 85

EXAMPLE 2: "2PAC and U2 have class" Numbers: // there aren't any numbers

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
int CharToInt(int c) {
  return abs('0' - c);
}
int MakeNumber(std::vector < int > temp) {
  int num;
  if (temp.size() == 1)
    num = temp[0];
  if (temp.size() == 2)
    num = temp[0] * 10 + temp[1];
  if (temp.size() == 3)
    num = temp[0] * 100 + temp[1] * 10 + temp[2];
  if (temp.size() == 4)
    num = temp[0] * 1000 + temp[1] * 100 + temp[2] * 10 + temp[3];
  return num;
}

std::vector < int > ExtractNumbers(std::string str) {
  std::vector < int > a;
  bool found_number;
  std::vector < int > temp;
  for (int i = 0; i < str.length(); i++) {
    // skip spaces
    found_number = false;
    while (str[i] == ' ' && i < str.length())
      i++;
    // inside word
    while (str[i] != ' ' && i < str.length()) {
      while (str[i] >= '0' && str[i] <= '9' && i < str.length()) {
        temp.push_back(CharToInt(str[i]));
        i++;
        found_number = true;
      }
      i++;
    }
    if (found_number)
      a.push_back(MakeNumber(temp));
    temp.clear();
  }
  return a;
}

int main() {
  std::vector < int > a = ExtractNumbers("120 brave students, 35 failed, remaining 85...");
  std::vector < int > b = ExtractNumbers("2PAC and U2 have class");
  for (int i: a) std::cout << i << " ";
  std::cout << std::endl;
  for (int i: b) std::cout << i << " ";
  return 0;
}

OUTPUT:

120 35 85 // ✅

2 2 // ❌

Could you help me to modify this to work correctly for the example 2? How could I check if there is space before and space/punctuation after the number is found?


Solution

  • I found a way to solve this.

    #include <iostream>
    #include <string>
    #include <vector>
    std::vector < int > extract_numbers(std::string s) {
      std::vector < int > vek;
      for (int i = 0; i < s.length(); i++) {
        std::string word;
        while (s[i] == ' ' && i < s.length()) i++;
        while (s[i] != ' ' && i < s.length()) word += s[i++];
        int j = 0;
        while (word[j] >= '0' && word[j] <= '9') j++;
        std::string spaces = ".,;!?";
        for (int i = 0; i < spaces.size(); i++)
          if (word[j] == spaces[i] || word[j] == '\0') {
            vek.push_back(std::stoi(word));
            break;
          }
      }
      return vek;
    }
    int main() {
      std::string s;
      std::getline(std::cin, s);
      for (int i: extract_numbers(s))
        std::cout << i << " ";
      return 0;
    }