473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

split number problem

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£º mi*****@hotmail.com
---------
Nov 13 '05 #1
16 3351
On Wed, 03 Sep 2003 15:05:30 +0800, nicolas wrote:
Aim: scanf a number what random digit,split this number;


This wouldn't by any chance happen to be your homework?

NPV
Nov 13 '05 #2
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.

--
PGP key 39A40276 at wwwkeys.eu.pgp.net
key fingeprint: 9A0B 61C6 B826 4B73 69BB 972B C5E7 A153 39A4 0276
Nov 13 '05 #3
nicolas wrote:
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;
}

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

Nov 13 '05 #4
nicolas wrote:

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.

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Nov 13 '05 #5


LibraryUser wrote:

nicolas wrote:

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.

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.


Kinda tough if I/O is the terminal. How about using gets() to get the
string, then use putc or printf to output.
--
Fred L. Kleinschmidt
Associate Technical Fellow
Boeing Common User Interface Services
Nov 13 '05 #6
nicolas wrote:
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£º mi*****@hotmail.com
---------


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[i]);

/* 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[i] = '\0';
return i;
}

--Steve

Nov 13 '05 #7

On Wed, 3 Sep 2003, Fred L. Kleinschmidt wrote:

LibraryUser wrote:
nicolas wrote:

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


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.


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

Nov 13 '05 #8
Morris Dovey <mr*****@iedu.com> wrote in message news:<Se**************@news.uswest.net>...
nicolas wrote:
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...


on a lighter note, are we "positive" /*:) */ that the input is an
unsigned integer to the base of 10 ? :)
Nov 13 '05 #9

"Dimitris Mandalidis" <ma****@mandas.org.remove_this_to_reply> wrote in
message
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?
Nov 13 '05 #10
Aishwarya wrote:
Morris Dovey <mr*****@iedu.com> wrote in message news:<Se**************@news.uswest.net>...
nicolas wrote:
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...


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^)
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

Nov 13 '05 #11
I get many benefit for solution and mothod via this note...

thanks :)

----from Chinese best regard.

"nicolas" <ni********@citiz.net> wrote in message
news:bj***********@mail.cn99.com...
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£º mi*****@hotmail.com
---------

Nov 13 '05 #12
Morris Dovey <mr*****@iedu.com> wrote in message news:<NS******************@news.uswest.net>...
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,
Nov 13 '05 #13
nicolas wrote:
I get many benefit for solution and mothod via this note...

thanks :)

----from Chinese best regard.

"nicolas" <ni********@citiz.net> wrote in message
news:bj***********@mail.cn99.com...
Aim: scanf a number what random digit,split this number;

for example: input "5680"

output "5,6,8,0";
input "45"

output "4,5";

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

--Steve


Nov 13 '05 #14
Aishwarya wrote:
:) there was certainly no offense intended ..


....and none was taken (I enjoyed your pun).
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

Nov 13 '05 #15
nicolas wrote:

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...
--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Nov 13 '05 #16
On Wed, 03 Sep 2003 15:25:10 GMT, Steve Zimmerman <st******@sonic.net>
wrote:
<snip>
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[i] = '\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
Nov 13 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: nieuws | last post by:
Hi, I was trying to do the following. It's my first php "project", so it's quiet logic that i have some problems. Perhaps the php community might help. It's about this : I have a txt file...
14
by: Luka Milkovic | last post by:
Hello, I have a little problem and although it's little it's extremely difficult for me to describe it, but I'll try. I have written a program which extracts certain portions of my received...
3
by: rxl124 | last post by:
Hi, room Beginner of learning perl here!! I have question to all, I have below file name datebook.master which contains only 2 lines Mike wolf:12/3/44:144 park ave, paramus: 44000 Sarah kim:...
9
by: martin | last post by:
Hi, a very newbie question. How do I split the adress and number to 2 variables? ex. "Kingsroad 347" = variabel1 = "Kingsroad" variabel2 = "347" Ill guess i have to search the string from...
2
by: mike | last post by:
In my array i have 10|5.98 string mySplit = myArray.Split('|'); string DeliveryAddress = mySplit; string ShippingID = mySplit; my problem is ShippingID contains 5.9 not 5.98 can anyone...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
2
by: Transcend2030 | last post by:
Hi, I'm having problems with string.split() My problem is; I have a sentence which the user inputs, I then input a word and the number of times that word appears in the sentence is displayed. ...
4
by: Gilberto | last post by:
Hello, I have a couple of forms using the code to FIND AS YOU TYPE from Allen Browne (http://allenbrowne.com/AppFindAsUType.html). It worked PERFECTLY until yesterday when i splitted the db into...
2
by: ogo796 | last post by:
Hi guys am having a problem with a split(),i retrieve line from the text file and i wanna split that line.i manage to split two words but splitting the string enclosed on brackets it seems to be a...
9
JustJim
by: JustJim | last post by:
G'day everybody, One of my clients' suppliers sent them a spreadsheet containing price increase data. For Ghu only knows what reason, they have a column with my client's part number and the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.