Advertisement
sailorbob74133

Maman 12 Q4 in C

Nov 19th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // max 10 digists for a 32 bit number plus enter
  4. char NUMBER[13];
  5.  
  6. // return length of a newline terminated string
  7. int strLen( char *a0 ) {
  8.     int t1 = 0;
  9.  
  10.     while ( *a0 != 10 ) {
  11.         t1++;
  12.         a0++;
  13.     }
  14.  
  15.     return t1;
  16. }
  17.  
  18. int main(void) {
  19.     int s1, s2, s3, t1;
  20.     while ( fgets(NUMBER, 12, stdin) != NULL ) {
  21.         s1 = strLen(NUMBER);
  22.         s2 = 1; // column
  23.         s3 = 0; // result
  24.                 t1 = 0; // temp
  25.  
  26.                 s1--; // adjust to be an index
  27.         while ( s1 != -1 ) {
  28.             // check that the char is a digit
  29.                         t1 = *(NUMBER+s1);
  30.             if ( t1 < '0' || t1 > '9' )
  31.                 goto error;
  32.                         t1 = t1 - '0';
  33.             s3 += t1 * s2;
  34.             s2 *= 10;
  35.             s1--;
  36.         }
  37.         printf("%d\n", s3);
  38.         if ( s3 >= 100 )
  39.                 break;
  40.     }
  41.     return 0;
  42.     error:
  43.     printf("error\n");
  44.     return 1;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement