![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2006
Posts: 1
Rep Power: 0
![]() |
Extracting numbers from a string
Hi there,
I'll get straight down to the point: I need my program to accept a line as input - that's simple: fgets does exactly that - but then I need to extract the numbers contained in the line into the appropriate variables. Say there are 3 numbers in the line, so the line looks like this: "23 43 54". I'd want to have a=23, b=43 and c=54. Is there any function that would extract the numbers for me? Or do I have to do it 'manually'? Thanks for any input, Ingek |
|
|
|
|
|
#2 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3
![]() |
try this for C: LINK
__________________
Quote:
|
|
|
|
|
|
|
#3 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>Or do I have to do it 'manually'?
'Manual' is more or less what C is about. So yes, fgets the line and pull out your trusty strtol to extract the numbers.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
I'm not sure on the correct syntax but look up Split. You'll call it like string.Split(" ") or something where String is the Var. What it does is create an array and splits the input
I'm not sure on this being the correct syntax but here is an idea: string MyInput, InputArray(); //declare the variables MyInput << Cin; //Assing the input to the var MyInput InputArray = MyInput.Split(" "); //Split MyInput at " " assign it to the array InputArray |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Ummmm, this is the C forum...please be careful when advising those seeking help.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 | |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,197
Rep Power: 5
![]() |
If I were you, I'd read it in manually like you do, then break it up into substrings and individually convert those. You can use strtok() to grab each substring, and then atoi(), strtol(), strtoul(), or strtod() to parse each number. You can read up on these functions here.
You can either grab all the substrings (you'll need a pointer variable to store each) and then parse the numbers, or you can grab the first substring, parse it, grab the next, and so on. The second method seems more robust to me, and means you'll only need a single pointer to store the substring, but you still need a place to put all the numbers. That begs the question: are you receiving an arbitrary quantity of numbers, or are you limiting it (and thus, tossing any extra numbers)? If you can expand the second method to also process the numbers (ie, substring then parse then process then toss), you might only need a single variable that you just re-use (only works it they're of a like type, mind you). Don't forget to test for errors in user input, either. I advise not using atoi(), as it returns zero on failure, leaving you not knowing if it returned a zero because it failed, or because the user actually entered a zero. The strto...() functions, on the other hand, return an error code, and fill in the number via a passed-in pointer, so they provide better error detection. Quote:
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
|
#7 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>I advise not using atoi(), as it returns zero on failure
Not to mention it invokes undefined behavior if the input string can't be represented as an integer.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I didn't call him on bad C++ code because there was little reason to presume it WAS C++ code. I've never heard of "assing the input." I am trying to be less cantankerous, yes; there is no assurance that I will succeed to the extent that those who emit asininities would like.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 9:44 PM |
| Median/Mode in arrays? {Need help} | Java|Tera | Java | 27 | Nov 29th, 2005 11:50 AM |
| Array issues :( | Alo Tsum | Java | 10 | Nov 26th, 2005 6:45 PM |
| replace space with ' * ' | TecBrain | C++ | 15 | Apr 13th, 2005 1:32 PM |
| function | solomon_13000 | Java | 6 | Apr 3rd, 2005 12:42 AM |