Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extracting digits from input string c++

Tags:

c++

parsing

I'm trying to do parsing to some input string reactions read from file at formula :2W+B=8A+10Z, I'm not interested in characters i need only to split and extract the integer values to put them in a vector i.e vector associated with the reaction here is :[2 1 8 10] i thought about many things: std::strtok(),isdigital(),find_first_of() but they all didn't work for integer values ... can any body help ??

here my try:

int main()
{
  std::string input;
  std::getline(std::cin, input);
  std::stringstream stream(input);
  while(1) {
      int n;
      stream >> n;
      char * pch;
      pch = strtok (input," ");
      while (pch != NULL)
        {
          printf ("%s\n",pch);
          pch = strtok (NULL, " ,.");
        }
  }
}
like image 836
Sama_science Avatar asked Jun 27 '26 22:06

Sama_science


2 Answers

This will do what you want in this particular case. However, i suggest that you look into regex to parse your equation better. You may want to consider all possible cases for your input. This includes \,-,* and other operators that you may want to add in your equation. Also, I'm assuming variables in your equation has only one character.

int main()
{
  string input;
  getline(std::cin, input);
  stringstream stream(input);

  char tmp[256];
  const char *in = input.c_str();
  char str[256];
  strcpy(str,in);
  int x;
  tmp[0]='\0';
  char c;
  vector<int> vec;
  //Scan for the digit
  //if it is, store the rest of the string back to str
  //if it isn't, store the part of the string before a digit to tmp
  while (sscanf(str,"%d%s",&x,str)  || sscanf(str,"%[^0123456789]%s",tmp,str) > 1)
    {
      //check if tmp has the form [variable name]+[a string]
      //a string can include another variable name and an operator, = in this case
      while(sscanf(tmp,"%c+%[^0123456789]",&c,tmp) > 1)
        vec.push_back(1);
      if (tmp[0]=='\0')
        vec.push_back(x);
      tmp[0]='\0';
    }

  //just in case there're more special cases
  while(sscanf(str,"%c+%[^0123456789]",&c,str) > 1)
    vec.push_back(1);

  for(int i = 0; i < vec.size(); i++)
    cout << vec[i] << endl;
}

Output:

2
1
8
10

See comments for explanation.

EDIT

Be careful when you have a special case 2W+B=8A+10Z+C+D. Notice the last C D should both have coefficients 1. This could happen in the middle of the equation too.

like image 175
user3813674 Avatar answered Jun 29 '26 12:06

user3813674


Here is another solution:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
  string equ;
  vector<int> digits;
  cout << "enter your equation: \n";
  cin >> equ;

  for (auto i : equ)
  {
      if (isdigit(i))
        {
          digits.push_back(stoi(string{i}));
      }
  }

  for (auto& i : digits)
  {
      cout << i << endl;
  }

  system("pause");
  return 0;
}
like image 42
sunny1304 Avatar answered Jun 29 '26 12:06

sunny1304



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!