How can I separate an integer into digits using for loop?
eg.
before: 123
after: 1 2 3
anyone can help me with that?
Ricky_v.s_C++ 0 Newbie Poster
Recommended Answers
Jump to PostHere is an idea. Convert to char *
Bad idea (unless you care to elaborate).
Often using division (/) and remainder (%) operations is used for homework.
Jump to PostSo a string, not a char*.
Avoid this idiom:
for (i =0; i<strlen(buff); i++)
And you don't really need to do
atoi
(which is declared in <stdlib.h>) if you just subtract '0'.
Jump to PostDon't understand what you trying to tell (my english is far from perfect) but whats wrong with strlen. You mean don't use it in for loop?
Right. As the loop condition, the
strlen
is called on each iteration of the loop. For longer strings, this makes something that looks [TEX]O(n)[/TEX] …
All 9 Replies
andor 25 Posting Whiz in Training
Dave Sinkula 2,398 long time no c Team Colleague
andor 25 Posting Whiz in Training
Dave Sinkula 2,398 long time no c Team Colleague
andor 25 Posting Whiz in Training
Dave Sinkula 2,398 long time no c Team Colleague
andor 25 Posting Whiz in Training
Dave Sinkula 2,398 long time no c Team Colleague
Ricky_v.s_C++ 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.