In C I guess that the best way is using strtok + atoi
int main() {
char s[] = "123 456 8 008 -12";
char *p = strtok(s," ");
while (p) {
printf("%d\n",atoi(p));
p = strtok(NULL," ");
}
return 0;
}
On Tue, May 18, 2010 at 2:24 AM, Carlos Guia <[email protected]> wrote:
>
> In C++ you can use string streams, you should include <sstream>
>
> /* You can use an actual string or char* as a parameter, not completely
> sure if it is iss = "2 3" or iss("2 3") */
> istringstream iss = "2 3 4";
> int x;
> while ( iss >> x ) {
> printf("%d\n", x);
> }
>
> Output would be:
> 2
> 3
> 4
>
> in plain C you could do something like this, there are probably better
> ways.
>
> char* ptr = "2 3 4"; /* again use actual line here */
> while ( *ptr != 0 ) {
> while ( *ptr != 0 && !isdigit(*ptr) ) ++ptr;
> /* this line is needed if the line may not end in a digit, i.e. "2 3 4
> " or "2 3 4\n" */
> if ( *ptr == 0 ) break;
> int x = 0;
> while ( isdigit(*ptr) ) {
> x = x * 10 + (*ptr - '0');
> ++ptr;
> }
> printf("%d\n", x);
> }
>
> same output as C++ version
>
> Hope it helps,
> Carlos Guía
>
>
>
> On Mon, May 17, 2010 at 3:41 AM, maverick gugu <[email protected]>wrote:
>
>> Hi All,
>> Sorry that I'm pulling an old problem up. With reference to GCJ 09,
>> round 1A problem a(Multi-base Happiness)
>> URL: http://code.google.com/codejam/contest/dashboard?c=188266#s=p0
>>
>> How does one accept an arbitrary set of integers as input?
>> -By reading an entire line and processing number by number is one
>> method. But the problem is for two digit numbers..
>>
>> Just checked the code of others, but would be really nice if someone can
>> explain it out more clearly in C/C++.(C would be great.)
>> Thank you.
>>
>> -Maverick
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "google-codejam" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<google-code%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/google-code?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-codejam" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-code%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-code?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-code?hl=en.