Search code examples
carraysstringintegerisbn

Converting Character Array to Integer Array in C for ISBN Validation


I really hope someone can give a well explained example. I've been searching everywhere but can't find a proper solution.

I am taking an introduction to C Programming class, and our last assignment is to write a program which validates a 10 digit ISBN with dashes... The ISBN is inputted as a string in a CHAR array. From there I need to separate each digit and convert them into an integer, so I can calculated the validity of the ISBN. On top of that, the dashes need to be ignored..

My thought process was to create an INT array and then use a loop to store each character into the array, and pass it through the atoi() function. I also tried using an IF statement to check each part of the CHAR array to see if it found a dash. If it did find one, it would skip to the next spot in the array. It looked something like this:

int num[12], i = 0, j = 0, count = 0;
char isbn[12];

    printf ("Enter an ISBN to validate: ");
    scanf ("%13[0-9Xx-]%*c", &isbn);

 do {
        if (isbn[i] == '-') {
            i++;
            j++;
        }
        else {
            num[i]= atoi(isbn[j]);
            i++;
            j++;
        }
        count++;
    } while (count != 10);

But that creates a segmentation fault, so I can't even tell if my IF statement has actually filtered the dashes....

If someone could try and solve this I'd really appreciate that. The Assignment was due Dec 4th, however I got an extension until Dec 7th, so I'm pressed for time.

Please write out the code in your explanation. I'm a visual learner, and need to see step by step.

There's obviously a lot more that needs to be coded, but I can't move ahead until I get over this obstacle.

Thanks in advance!


Solution

  • First of all, your definition of isbn is not sufficient to hold 13 characters; it should therefore be 14 chars long (to also store the terminating '\0').

    Second, your loop is overly complicated; three loop variables that maintain the same value is redundant.

    Third, the loop is not safe, because a string might be as short as one character, but your code happily loops 10 times.

    Lastly, converting a char that holds the ascii value of a digit can be converted by simply subtracting '0' from it.

    This is the code after above improvements have been made.

    #include <stdio.h>
    
    int main(void)
    {
        int num[14], i;
        char isbn[14], *p;
    
        printf("Enter an ISBN to validate: ");
        scanf("%13[0-9Xx-]%*c", &isbn);
    
        // p iterates over each character of isbn
        // *p evaluates the value of each character
        // the loop stops when the end-of-string is reached, i.e. '\0'
        for (p = isbn, i = 0; *p; ++p) {
            if (*p == '-' || *p == 'X' || *p == 'x') {
                continue;
            }
            // it's definitely a digit now
            num[i++] = *p - '0';
        }
    
        // post: i holds number of digits in num
        // post: num[x] is the digit value, for 0 <= x < i
    
        return 0;
    }