split number problem

N

nicolas

Aim: scanf a number what random digit,split this number;

for example: input "5680"

output "5,6,8,0";


input "45"

output "4,5";

--


_________________________________________
ÎÒÈÈ°®ÉîÈëµÄÌÖÂÛ£¡
ÓÐÊÂÇëͬÎÒÁªÏµ£¬
MSN£º (e-mail address removed)


---------
 
D

Dimitris Mandalidis

On Wed, 3 Sep 2003 15:05:30 +0800, nicolas wrote
Aim: scanf a number what random digit,split this number;

for example: input "5680"

That's easy :

scanf("%d", &number);
number_of_digits = (int) (log10(number))+1; /* include <math.h> link with -lm */
if ((string = malloc(number_of_digits + 1)) == NULL)
/* Error handling */
token = string;
for (i = number_of_digits - 1; i >= 0; --i)
{
*string++ = (int) (number / pow(10,i)) + '0';
number = number % (int) pow(10,i);
*string++ = ',';
}
*string = '\0';
printf("%s\n", token);

HTH
D.
 
M

Morris Dovey

nicolas said:
Aim: scanf a number what random digit,split this number;

for example: input "5680"
output "5,6,8,0";

input "45"
output "4,5";

Nicolas...

If you preferred not to use the functions prototyped in math.h,
then you could do something like:

#include <stdio.h>
#include <stdlib.h>

char *unsigned_to_digits(unsigned n)
{ char *p;
unsigned digits = 0,copy = n;
do ++digits; while (n /= 10); /* :) */
if (p = malloc(2*digits))
{ p += 2*digits;
*--p = '\0';
do
{ *--p = '0' + copy % 10;
if (copy /= 10) *--p = ',';
} while (copy);
}
return p;
}

int main(void)
{ char *p;
unsigned value;
scanf("%u",&value);
puts((p = unsigned_to_digits(value)) ? p :
"malloc() failure");
return 0;
}
 
L

LibraryUser

nicolas said:
Aim: scanf a number what random digit,split this number;

for example: input "5680"
output "5,6,8,0";

input "45"
output "4,5";

Don't use scanf, use getc and putc.
 
F

Fred L. Kleinschmidt

LibraryUser said:
Don't use scanf, use getc and putc.

Kinda tough if I/O is the terminal. How about using gets() to get the
string, then use putc or printf to output.
 
S

Steve Zimmerman

nicolas said:
> Aim: scanf a number what random digit,split this number;
>
> for example: input "5680"
>
> output "5,6,8,0";
>
>
> input "45"
>
> output "4,5";
>
> --
>
>
> _________________________________________
> ÎÒÈÈ°®ÉîÈëµÄÌÖÂÛ£¡
> ÓÐÊÂÇëͬÎÒÁªÏµ£¬
> MSN£º (e-mail address removed)
>
>
> ---------
>
>
>

This works, but it doesn't use scanf:

#include <stdio.h>

int read_line(char str[], int n);

int main()
{
char digits[100];
int how_many;
int i;

printf("Enter up to 100 digits: ");
how_many = read_line(digits, 100);

for(i = 0; i < (how_many - 1); i++)
printf("%c,", digits);

/* Print the last digit with no trailing comma: */
printf("%c", digits[how_many - 1]);

printf("\n");

return 0;
}

int read_line(char str[], int n)
{
char ch;
int i = 0;

while ((ch = getchar()) != '\n')
if (i < n)
str[i++] = ch;

str = '\0';
return i;
}

--Steve
 
A

Arthur J. O'Dwyer

Kinda tough if I/O is the terminal. How about using gets() to get the
string, then use putc or printf to output.

How about using getc and putc? (Because gets is evil, and printf is
useless in the context of this problem.)

Hint: If the user enters "47227", what's the output?
What's the fastest way to generate this output?

(Rhetorical hints, BTW. Please don't do the OP's homework any
more than we've already done... Nice "solutions," by the way,
guys!)

Oh, and probably the OP can't use getc IRL, because the teacher
has told them specifically to use scanf. (Not that scanf("%c")
behaves much differently to getc(stdin), of course...)

-Arthur
 
S

Serve La

Dimitris Mandalidis said:
scanf("%d", &number);
number_of_digits = (int) (log10(number))+1; /* include <math.h> link
with -lm */

what should happen if you pass in zero or a negative value?
 
M

Morris Dovey

Aishwarya said:
on a lighter note, are we "positive" /*:) */ that the input is an
unsigned integer to the base of 10 ? :)

Aishwarya...

Of course not; but I hesitate to provide complete, general
solutions to homework problems. /My/ aim was to provide the OP
with something he could understand and modify as necessary to
meet the requirements of his assignment - to provide help without
eliminating the need for effort from the student.

It's a difficult balance to achieve, sometimes. It's easy to
either say: "Do your own homework!" or to provide the complete
solution. I find it much more difficult (but enormously more
satisfying) to provide a rough solution framework that a student
can hack on.

The modifications necessary to handle signed ints aren't really
difficult; and if output radix is an issue, the OP forgot to
mention it; and will have to further (trivially) modify the code.

A loser will hand in my code and almost certainly be identified
as submitting someone else's work - or they'll recognize that the
solution is incomplete and disregard it altogether. A winner will
note the use of "unsigned", work to understand what I've done,
and puzzle out the necessary modifications - and thereby make the
resulting program a product of his own efforts.

I always hope for winners.

8^)
 
N

nicolas

I get many benefit for solution and mothod via this note...

thanks :)

----from Chinese best regard.
 
A

Aishwarya

Morris Dovey said:
Aishwarya...

Of course not; but I hesitate to provide complete, general
solutions to homework problems. /My/ aim was to provide the OP
with something he could understand and modify as necessary to
meet the requirements of his assignment - to provide help without
eliminating the need for effort from the student.

It's a difficult balance to achieve, sometimes. It's easy to
either say: "Do your own homework!" or to provide the complete
solution. I find it much more difficult (but enormously more
satisfying) to provide a rough solution framework that a student
can hack on.

The modifications necessary to handle signed ints aren't really
difficult; and if output radix is an issue, the OP forgot to
mention it; and will have to further (trivially) modify the code.

A loser will hand in my code and almost certainly be identified
as submitting someone else's work - or they'll recognize that the
solution is incomplete and disregard it altogether. A winner will
note the use of "unsigned", work to understand what I've done,
and puzzle out the necessary modifications - and thereby make the
resulting program a product of his own efforts.

I always hope for winners.

8^)

Morris,

I do appreciate and admire your thoughts. But as I said earlier, it
was just a light note :) there was certainly no offense intended ..

Cheers,
 
S

Steve Zimmerman

nicolas said:
I get many benefit for solution and mothod via this note...

thanks :)

----from Chinese best regard.


You're welcome, Nicolas, and thank you for the question.
If you have any more questions, please send them in to
this group.

--Steve
 
C

Charles Richmond

nicolas said:
Aim: scanf a number what random digit,split this number;

for example: input "5680"

output "5,6,8,0";

input "45"

output "4,5";
Just input the number as a string, and scan down
the string with a "for" loop. Output each character
that is a number...
 
D

Dave Thompson

int read_line(char str[], int n)
{
char ch;
int i = 0;

while ((ch = getchar()) != '\n')
if (i < n)
str[i++] = ch;
Infinite loop if input EOF or error occurs.
str = '\0';


Stores out of bounds (causing Undefined Behavior) if input provides n
or more characters in one line.
return i;
}

--Steve

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top