-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshiftingnumbers.c
64 lines (56 loc) · 1.85 KB
/
shiftingnumbers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* AUTHOR: Khaled Mohammad
* DATED: September 5, 2015.
* git: https://github.com/itskhaledmohammad
* twitter: https://twitter.com/itskhaledmd (@itskhaledmd)
* This is a very simple challenge which was given to be solved
* in a programmer recruitment interview.There is only one rule.You
* can use only one loop for sorting the numbers.
*/
#include <stdio.h>
#include <stdlib.h>
/*
* This functions displays the content
* of an given array.
*/
void displayarr(int sizeA, int arr[])
{
// Going through each element of the array and printing it.
for(int i = 0; i < sizeA; i++){
printf("%d ", arr[i]);
}
printf("\n\n"); // Printing the lines.
}
/*
* This function splits a string of number
* into individual digits and saves
* it to an array.
*/
void splitInput(int arr[], int sizeArr, char num[])
{
for(int i = 0; i < sizeArr; i++)
// We are subtracting 48 because the numbers in ASCII starts at 48.
arr[i] = (int)num[i] - 48;
}
int main(void)
{
// Initialization.
int numArr[11] , pos = 0;
int arrSize = sizeof(numArr) / sizeof(int);
char number[11];
// Take input.
printf("Enter your number > ");
scanf("%s", number);
splitInput(numArr, arrSize, number);
// Display our Current array.
printf("Our Current Array: ");
displayarr(arrSize, numArr);
// Shifting the non zero numbers to front.
for(int i = 0; i < arrSize; i++){
if(numArr[i] != 0){ // Checking if it is a non-zero number.
numArr[pos] = numArr[i]; // Putting the non-zero number in position of our cursor.
numArr[i] = 0; // Putting zero in the previous place of the non-zero number.
pos++; // Setting our cursor to the next position.
}
}
displayarr(arrSize, numArr); // Display our sorted array.
}