0

I am facing problem to split my input in C++, for something similar to the Python split function.
The input is given as 1001-43 1003-45 1008-67 in different lines. I want to know how to take these inputs split by '-' and store them in different variables.

In Python it's:

a, x = input().split('-')
Eitan T
  • 32,660
  • 14
  • 72
  • 109
Harshit
  • 1,207
  • 1
  • 20
  • 40
  • 1
    possible duplicate of [How to split the strings in vc++?](http://stackoverflow.com/questions/1044088/how-to-split-the-strings-in-vc) – Bo Persson Jul 01 '12 at 09:02

3 Answers3

1

Have a look at boost. The string algorithms library includes most of what you can find in python including a split function which splits a string into an stl container of your choice. For example (lifted from their docs) splitting on dash or asterisk:

std::string str1("hello abc-*-ABC-*-aBc goodbye");

std::vector< std::string > SplitVec; // #2: Search for tokens
split( SplitVec, str1, is_any_of("-*"), token_compress_on );

// SplitVec == { "hello abc","ABC","aBc goodbye" }
Alex Wilson
  • 6,690
  • 27
  • 44
-1
int number,digit1,digit2,digit3;
std::cin>>number;
digit1=number%10;
digit2=number%100;
digit3=number%1000;
-2

Check out strtok(), http://www.cplusplus.com/reference/clibrary/cstring/strtok/

Digital Da
  • 891
  • 1
  • 10
  • 23