Re: Converting a substring to Integer
On Feb 6, 9:33 pm, "Daniel T." <danie...@earthlink.net> wrote:
> ReeteshMukul <reetesh.mu...@gmail.com> wrote:
>
> > Actually, I assumed that numeric-digits will appear surrounded by
> > space. This means some thing like {alphabet}*space {digits}+
> > space({alphabet}*space)*. Your code will extract digits in the
> > following conditions also:- djshdjshj14524 jdhdss
>
> I still don't see why you had to make the procedure so complex. Also
> note, if presented with "5x 7", your code will extract the '5', not the
> '7'.
>
> Your code is emulated pretty well by the code below, which is far
> simpler:
>
> int main()
> {
> std::string s = "dsdhjsahdk dsdjdsaj 36782367 sdjdhak";
>
> int result = 0;
> std::stringstream cstr( s );
> char ch = 0;
> while ( cstr >> ch && !isdigit( ch ) )
> cstr.ignore( numeric_limits<streamsize>::max(), ' ' );
> cstr.putback( ch );
> cstr >> result;
> cout << result;
>
> }
Yes, this is a great solution. Very simple and compact solution.
Regards,
Reetesh Mukul
|