hi can someone show me how to split numbers?
for example
12345678910
the output will be 12 34 56 78 910
can some one give me some idea?please?
This is a discussion on split number in two.. within the C++ Programming forums, part of the General Programming Boards category; hi can someone show me how to split numbers? for example 12345678910 the output will be 12 34 56 78 ...
hi can someone show me how to split numbers?
for example
12345678910
the output will be 12 34 56 78 910
can some one give me some idea?please?
substr() ?
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
You can use 'stringstreams':
Code:#include <iostream> #include <string> #include <sstream> using namespace std; int main() { int num = 123456; //Turn num into a string: ostringstream myOutputStr; myOutputStr<<num; string myStr = myOutputStr.str(); //Get a substring of num: string str1 = myStr.substr(0,2); //If you need to, turn the substring into an int: istringstream myInputStr(str1); int num1 = 0; myInputStr>>num1; //Display the result: cout<<num1<<" * 2 = "<<num1 * 2<<endl; return 0; }
Last edited by 7stud; 04-01-2006 at 10:15 AM.
Question #1: *how* do you want to split them?
(Off Topic) Sorry but just had to ask, to 7stud:
Is using stringstreams better (faster) then using itoa and atoi to convert integers to strings and the way arround?
If so, why?
>> Is using stringstreams better (faster) then using itoa and atoi to convert integers to strings and the way arround? If so, why?
Does speed denote betterness? There are many qualifications for good code.
itoa() isn't part of the C++ standard. MS made it up.Is using stringstreams better (faster) then using itoa and atoi to convert integers to strings and the way arround?
Last edited by 7stud; 04-01-2006 at 08:21 PM.
atoi is part of C89...
There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.
Yep, I was researching that, then I changed my post, and then I saw your post.![]()