Permalink
Browse files

Added ocr.c

  • Loading branch information...
1 parent 133185c commit 83ed273a44eac61ed1862dc506495bc3e38a2ff8 Edward Patel committed Feb 8, 2010
Showing with 56 additions and 0 deletions.
  1. +56 −0 C/ocr/ocr.c
View
@@ -0,0 +1,56 @@
+/* ======================================================================
+ *
+ * Author: Edward Patel, Memention AB
+ *
+ * ====================================================================== */
+
+#include <stdio.h>
+#include <string.h>
+
+// ----------------------------------------------------------------------
+
+int ocr_cc(const char *str) {
+ int c;
+ int v = 0;
+ int m = (strlen(str)%2);
+
+ while (*str) {
+ c = (*str - '0')*(m+1);
+ while (c>9)
+ c-=9;
+ v += c;
+ m = !m;
+ str++;
+ }
+
+ if (v%10)
+ return 10-(v%10);
+ return 0;
+}
+
+int ocr_ccl(const char *str) {
+ char buf[256];
+ char lc[2];
+
+ strcpy(buf, str);
+
+ lc[0] = '0' + ((strlen(str)+2)%10);
+ lc[1] = '\0';
+
+ strcat(buf, lc);
+
+ return ocr_cc(buf);
+}
+
+// ----------------------------------------------------------------------
+
+int main(int argc, char *argv[]) {
+
+ if (argc!=2)
+ return -1;
+
+ printf(" Only cc: %s%d\n", argv[1], ocr_cc(argv[1]));
+ printf("With len+cc: %s%d%d\n", argv[1], (int)(strlen(argv[1])+2)%10, ocr_ccl(argv[1]));
+
+ return 0;
+}

0 comments on commit 83ed273

Please sign in to comment.