I want to convert int array to string and reverse it (question no 2) and I can't seem to make it work and have no idea how to fix it. I used sprintf
to convert my int array to string but it split out random numbers.
question no 2 is based on question no 1 so the getsum is still needed here
the "getsum" is for questions no 1 and 2
the picture for the question is below
the question
(Q.1)An integer n is divisible by 9 if the sum of its digits is divisible by
9.
Develop a program to display each digit, starting with the rightmost digit.
Your program should also determine whether or not the number is divisible by
9. Test it on the following numbers:
n = 154368
n = 621594
n = 123456
Hint: Use the % operator to get each digit; then use / to remove that digit.
So 154368 % 10 gives 8 and 154368 / 10 gives 15436. The next digit extracted
should be 6, then 3 and so on.
(Q.2) Redo programming project 1 by reading each digit of the number to be tested
into a type char variable digit. Display each digit and form the sum of the
numeric values of the digits. Hint: The numeric value of digit is
(int) digit - (int) '0'
the code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int reverse(int n) {
char str[100];
sprintf(str, "%d", n);
int d = atoi(_strrev(str)); //str>int//
int arr[100];
int i = 0;
int display = 0;
char digit[100];
while (d != 0) {
display = d % 10;
arr[i] = display;
i ;
d = d / 10;
sprintf(digit, "%d", arr[i]); //int>char!!!//
}
for (i = i - 1; i >= 0; i--) {
printf("%s\n", digit);
}
return 0;
}
int getsum(int n) {
int sum = 0;
while (n != 0) {
sum = sum n % 10;
n = n / 10;
}
return sum;
}
int main() {
int n;
int i = 0;
printf("input n: ");
scanf("%d", &n);
printf("%d\n", reverse(n));
printf("%d\n", getsum(n));
return 0;
}
I also want to clarify that I'm new to StackOverflow in terms of asking questions, so if I did something wrong or didn't follow the required format I'm sorry :D
CodePudding user response:
If I'm correct, the line sprintf(digit, "%d", arr[i])
overwrites the buffer digit
each time you call it, and, therefore, you get a wrong answer in the end. You can use the return value of your first sprintf()
which is how many symbols have been written and the hint from your task. We get
// Code
int ndigits = sprintf(str, "%d", n);
// Code
char number[100];
number[ndigits--] = '\0';
while (d != 0) {
display = d % 10;
i ; // Now there's no need for this line. The number of digits is already
// counted
d = d / 10;
number[ndigits] = display '0'; // Use the hint
}
UPD: you can also use the malloc()
version
char *number = malloc(ndigits 1); // Add one for the null terminator
// Same code
free(number); // Free in the end
CodePudding user response:
You don't need to reverse the string. Also, for converting an int
digit to char
digit, you just need to do char_digit = int_digit '0'
.
Also in your code there was a bug in the for
loop, you should use different variable to loop (like j
).
int reverse(int n) {
int d = n, i = 0;
char digit[100] = {0}; // will initialize digit with zeros
while (d != 0) {
digit[i] = (d % 10) '0'; // '0' 1 = '1', '0' 4 = '4'
d = d / 10;
i ;
}
for (int j = i - 1; j >= 0; j--) {
printf("%c", digit[j]);
}
printf("\n");
return 0;
}
If you want to print the digits of n
in reverse, i.e. if n
is 2345 and you want to print 5432, then just change the for
loop:
for (int j = 0; j < i; j ) {
printf("%c", digit[j]);
}
printf("\n");