Sha256: c99211be3bbc984591ce8fbfa49c15ccd9281ca3211e9cb3f9423eae286a5a73

Contents?: true

Size: 732 Bytes

Versions: 71

Compression:

Stored size: 732 Bytes

Contents

#include "luhn.h"
#include <stddef.h>
#include <string.h>
#include <ctype.h>

bool luhn(const char *num)
{
   if (num == NULL)
      return false;

   int pos = strlen(num) - 1;
   if (pos < 0)
      return false;

   int res = 0;
   int every_second = 0;
   int zeros = 0;
   while (pos >= 0) {
      if (isdigit(num[pos])) {
         char c = num[pos] - '0';
         zeros += (c == 0);
         if (every_second) {
            c *= 2;
            res += c > 9 ? c - 9 : c;
         } else {
            res += c;
         }
         every_second = !every_second;
      } else if (num[pos] != ' ') {
         return false;
      }
      pos--;
   }
   if (res == 0 && zeros == 1)
      return false;

   return (res % 10 == 0);
}

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.179 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.178 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.177 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.176 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.175 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.174 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.173 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.172 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.171 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.170 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.169 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.167 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.166 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.165 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.164 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.163 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.162 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.161 tracks/c/exercises/luhn/src/example.c
trackler-2.2.1.160 tracks/c/exercises/luhn/src/example.c