Are you sure? It works fine for me.
My code (with a printf for debugging):
Code:
#include <stdio.h>
int i_summultofthree(int i)
{
int sum; char s[256];
sum = 0;
sprintf(s, "%d", i);
for(i=0;s[i]!=0; i++)
{
if(s[i]%3 == 0)
{
printf("found %c to be divisible by 3, summing\n", s[i]);
i = s[i] - '0';
sum = sum + i;
}
}
return sum;
}
int main()
{
int i;
i = 123456789;
printf("%d\n", i_summultofthree(i));
return 0;
}
My output:
Code:
found 3 to be divisible by 3, summing
found 6 to be divisible by 3, summing
found 9 to be divisible by 3, summing
18
Notice also that I have changed 48 to '0'. Using 48 doesn't buy you anything, and renders your code dependent on the ASCII character set. The compiler will translate '0' to the correct value for your character set anyway, and it's easier to understand.
Also, main needs to return int, I have fixed this.