954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pulling numbers and symbols from a String

I have a string that has a ton of random characters (pulled from a webpage). I want to pull all of the numbers, commas, hyphens, periods, and percentages from the page and put them into a text file.

I'm new to C++ so I'm not sure how I'd go about doing this. I'm pretty sure I can write to a text file, but pulling the numbers and those symbols has been a bit of a struggle.

How should I do this? What's the best practice method? Please don't just link me to a help site, I've scoured the internet already. :)

Thank you so much in advance!

Superandomness
Newbie Poster
5 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

No great trick to it; you just have to think about how you'd do it yourself and then turn that into an algorithm.

Something like:

1) Start looking at string, one character at a time. Start at end of string and work backwards, so that you'll know when you're scanning a percentage (assuming that percentages are of the form xx%).
2) If character is a comma, hyphens or period, copy to file.
3) If character is a percentage or a number, scan further until found all the character making up that percentage or number and copy to file.
4) Keep going until all characters examined.

Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
 

No great trick to it; you just have to think about how you'd do it yourself and then turn that into an algorithm.

Something like:

1) Start looking at string, one character at a time. Start at end of string and work backwards, so that you'll know when you're scanning a percentage (assuming that percentages are of the form xx%). 2) If character is a comma, hyphens or period, copy to file. 3) If character is a percentage or a number, scan further until found all the character making up that percentage or number and copy to file. 4) Keep going until all characters examined.

I am early enough in C++ that looking at characters is a bit of a mystery to me. Is there a method like the Java method nextInt()? I feel like I understand the logic of it, but not how to execute it.

I need full numbers, not just single digit numbers.
There's a few rules I have to go by like a comma between two numbers must be removed so the digits are put together. For example, "3,234" is parsed as 3234. And a hyphen preceding a numerical digit is treated as the negative sign unless it follows an English letter, another hyphen, or another digit. For instance, "greater than -3" is parsed as the number -3, "IIS-915876" as the number 918576, and "2 - 3" as two numbers 2 and 3.
There's a few other crazy rules like these.

So should I use an iterator? Is there a class like that I can use?

Sorry for all of the blubbering. I'm just very lost at what I should do.

Thank you!!

Superandomness
Newbie Poster
5 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

I would declare

vector <char> charv ; 
vector<char>::iterator cit ;

And then use strchr (string.h function) to locate the character you want.
If I remember correctly it will return a pointer to the location of that character and then you can 1) copy it into charv

charv.push_back(*pch) ;

and 2) delete it from the original string. A string is just an array of characters so you should be able to access it as one as well. The reason you want to pull it out is so when you go through it again you will not find the same character and then insert it twice into the same text file.

As far as writing it to a text file just look up methods in the fstream library. Haven't done a ton of file writing in c++ (mainly just c), but I'm fairly sure you can cout to a file...maybe not...hope this helps.

baldwindc
Light Poster
35 posts since Nov 2010
Reputation Points: 10
Solved Threads: 3
 

This was all very helpful, thank you!

Superandomness
Newbie Poster
5 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: