CFanatic

Go Back   CFanatic > Programming > C Programming

Coding help for C programming topic posted under C Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 08-18-2012, 04:01 PM
Junior Member
 
Join Date: Aug 2012
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
sumedh yadav is on a distinguished road
| More
Exclamation Coding help for C programming

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
Reply With Quote
  #2  
Old 08-19-2012, 01:51 AM
hobbyist's Avatar
Senior Member
 
Join Date: Jan 2012
Posts: 122
Thanks: 5
Thanked 49 Times in 42 Posts
hobbyist will become famous soon enough
| More
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
Before writing and testing the function, try strtol directly to make sure you get the expected result.

Code:
char *p;
printf("%ld", strtol("01001010101", &p, 2));
Give it a try and post your code if you need any help.
Good luck.
Reply With Quote
The Following User Says Thank You to hobbyist For This Useful Post:
sumedh yadav (08-19-2012)
  #3  
Old 08-19-2012, 02:49 AM
Junior Member
 
Join Date: Aug 2012
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
sumedh yadav is on a distinguished road
| More
Re: Coding help for C programming

till now we at are not taught use of arrays,strings.we are just being taught loops,if & else statements,functions.
Reply With Quote
  #4  
Old 08-19-2012, 02:16 PM
Junior Member
 
Join Date: Aug 2012
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
sumedh yadav is on a distinguished road
| More
Re: Coding help for C programming

till now we at are not taught use of arrays,strings.we are just being taught loops,if & else statements,functions.
Reply With Quote
  #5  
Old 08-19-2012, 08:36 PM
hobbyist's Avatar
Senior Member
 
Join Date: Jan 2012
Posts: 122
Thanks: 5
Thanked 49 Times in 42 Posts
hobbyist will become famous soon enough
| More
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.
Reply With Quote
  #6  
Old 08-21-2012, 10:35 AM
Junior Member
 
Join Date: Aug 2012
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
sumedh yadav is on a distinguished road
| More
Re: Coding help for C programming

thanks i got the logic and arrived at the answer
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Coding help for C programming sumedh yadav Introduce yourself 0 08-18-2012 12:56 AM
compiling coding in c Richa Introduce yourself 2 02-29-2012 07:07 AM
Hai I need a coding in C MANIKANDAN C Programming 0 01-01-2011 03:12 AM
Coding Standards diagramatic MFC Programming 0 10-04-2010 06:28 PM
Coding meyup C++ Programming 2 03-22-2010 11:30 PM



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.