![]() |
|
|||||||
![]() |
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Consider an integer in which each digit is either 0 or 1. Write a program to rearrange the digits such that all the 0s appear before the 1s. Output the resulting number.
sample input: 01001010101 sample output: 00000011111 |
|
#2
|
||||
|
||||
|
Re: Coding help for C programming
I think I'd try something like reading the user input into a char array, parsing the input string making sure it's either a 1 or 0, and if it is, process the string within a function.
Code:
char input[33] = { 0 }, *p;
long result = 0;
// read input;
// validate input;
// process input;
result = strtol(input, &p, 2);
// print result
Code:
char *p;
printf("%ld", strtol("01001010101", &p, 2));
Good luck. |
| The Following User Says Thank You to hobbyist For This Useful Post: | ||
sumedh yadav (08-19-2012) | ||
|
#5
|
||||
|
||||
|
Re: Coding help for C programming
what you might try is a couple of int variables for counters - one for 1s and another for 0s. If you use getchar for the input loop, you should be able to determine the number of 1s or 0s by using input - '0'.
After exiting the input loop, use the two counters in loops to print the respective 0s and then 1s. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
|
|||||||||||||||||||||||||||||||||||