how many times does i occur in x (unsigned 32 bit int C) -
this little bit confusing me, have see how many times occurs in x. if types 3 , x 4294967295, should 0 times, if types in 9 , 4294967295 x, should 3 times.
this have, output says 0 9, it's not working ..
int main(int argc, char** argv) { unsigned int i; scanf("%u", &i); unsigned int x; scanf("%u", &x); int output = 0; int t = 0; while (t < 10) { x /= 10; if (i == x) { output++; } t++; } printf("%d", output); }
int main(int argc, char** argv) { unsigned int i; scanf("%u", &i); if(i > 9) { // check it's single digit printf("expecting single digit\n"); return 1; // indicate failure } unsigned int x; scanf("%u", &x); int output = 0; // initialized output, removed t (use x directly instead) if(x == 0) { // special case if x 0 if(i == 0) { output = 1; } } else { while(x > 0) { // while there @ least digit if(i == (x % 10)) { // compare last digit output++; } x /= 10; // remove last digit } } printf("%d\n", output); // added \n sure it's displayed correctly return 0; // indicate success } also recommend using more explicit variable names, digit , number instead of x , i.
Comments
Post a Comment