Tratto da un libro
Converting an integer value to a string of characters takes even more effort. The algorithm for the conversion is the following:
1- Initialize a string to the empty string.
2- If the integer value is zero, then output a 0, and the algorithm is complete.
3- Divide the current integer value by ten, computing the remainder and quotient.
4-Convert the remainder (always in the range 0..9) to a character, and concatenate the character to the end of the string.
5- If the quotient is not zero, make it the new value and repeat steps 3-5.
6- Output the characters in the reverse order they were placed into the string.
Ho provato a fare
codice:
int i=0,a,d=0;
char str2[3];
for(a=2;a>=0 && i%10!=0;a--){
d=i%10;
i/=10;
str2[a]=(char)d;
}
printf("Stringa -> %s\n\n",str2);
Ma mi ritornano caratteri strani, dove ho sbagliato?