Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove leading % from integer

Tags:

c++

Just a simple problem here: I have a char** argv[] that holds all of my arguments...in one of these arguments, I get an integer proceeded by a %

For example:

bg %2

I really just want the integer....is there an easy way to get this?

This is for homework, so I am willing to do some more digging if anyone can prod me in the right direction.

Thanks

like image 386
Mizmor Avatar asked Mar 14 '26 05:03

Mizmor


1 Answers

Here is a way to do it using c++ methods:

lets assume you have one of the char* in the list char** argv[]

std::string tempString(argv[the one with the %]);
int position = tempString.find_first_of('%');
int = atoi(tempString.substr(position, tempString.size()-position).c_str());

A quick explination, the first line converst the char* into a std::string, the second line gets the position of the %, the third line gets the sub-string of the number (assuming it ends at the end of the char*), converts it back to a char* and passes it through atoi to get the int.

Hope this helps.

like image 156
Fantastic Mr Fox Avatar answered Mar 16 '26 18:03

Fantastic Mr Fox



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!