Guest User

Untitled

a guest
Nov 15th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. /* Check digit generator, for comp. sci. AS level example
  2. * Ashley Dixon, 2018 */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8.  
  9. int main ( int argc, char ** argv ) {
  10. const int weights[] = { 1, 3 };
  11. unsigned int len = 0, sum = 0;
  12. char * in = NULL;
  13.  
  14. if ( argc < 2 ) {
  15. fprintf ( stderr, "error: no arguments provided\n" );
  16. return EXIT_FAILURE;
  17. }
  18.  
  19. in = argv[1];
  20. len = strlen ( in );
  21.  
  22. for ( unsigned int i = 0; i < len; i++ ) {
  23. if ( !isdigit ( in[i] ) ) {
  24. fprintf ( stderr, "error: %c is not a valid digit (0-9)\n", in[i] );
  25. return EXIT_FAILURE;
  26. }
  27.  
  28. sum += weights[ i & 1 ] * ( in[i] - '0' );
  29. }
  30.  
  31. printf ( "digit: %d\n", 10 - ( sum % 10 ) );
  32. return EXIT_SUCCESS;
  33. }
Add Comment
Please, Sign In to add comment