In this exercise he asks me to create a function
Number_pos (N, pos, m)
which allows to extract a number composed of m
digits
from position pos
using functions.
Example:
N = 12345, pos = 2, m = 3
Number_pos (N, pos, m) = 234
I use an Extraxt_from_position
function which extracts a number from a given position, then I use a function which calculates the number of digits of the number to extract, then I have a mirror function which inverts the number and I do the successive division until the number of digits are equal to the number of digits of the number we want to extract.
The problem is: forbidden to use mirror function, can you help me
int Extract_from_position(int n, int r)
{
int m = 0, s = 0;
while (n != 0)
{
m = n % 10;
s++;
if (s == r)
{
return n;
}
n = n / 10;
}
}
int Number_of_digits(int n)
{
int m = 0, s = 0;
while (n != 0)
{
m = n % 10;
s++;
n = n / 10;
}
return s;
}
int Mirror(int n)
{
int m = 0, s = 0;
while (n != 0)
{
m = n % 10;
s = s * 10 + m;
n = n / 10;
}
return s;
}
int Number_Pos(int N, int pos, int m)
{
int x = Extract_from_position(N, pos);
int y = 0;
int R = Mirror(x);
int T = Number_of_digits(R);
while (T >= m + 1)
{
y = R % 10;
R = R / 10;
T--;
}
return Mirror(R);
}
int main()
{
int n, pos, nbcx;
printf("Give n :");
scanf("%d", &n);
printf("Give the position :");
scanf("%d", &pos);
printf("give the number of digits of the number to extract :");
scanf("%d", &nbcx);
printf("\nThe result after the amber extract from position %d on the right and the number of digits %d is : %d \n", pos, nbcx, Number_Pos(n, pos, nbcx));
}