A Way For Learning

Validating ISBN

No comments
Validating ISBN: Write a C program to define a structure Book with the attributes, book title,
author, subject, isbn number, Based on the given input values display the books information if the
given ISBN is valid.
Following are the rules to verify whether the given ISBN is a valid or not.
The 2001 edition of the official manual of the International ISBN Agency says that the ISBN-10
check digit – which is the last digit of the ten-digit ISBN – must range from 0 to 10 (the symbol X is
used for 10), and must be such that the sum of all the ten digits, each multiplied by its (integer)
weight, descending from 10 to 1, is a multiple of 11.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

_Bool isValidISBN(long);
int getLength(char*);

int main(int argc, char *argv[]) {
 struct Book {
  char *bookTitle;
  char *bookAuthor;
  char *subject;
  long isbn;
 };

 struct Book book[5];
 int n;
 char *s = (char *)malloc(sizeof(char)*100);
 int length = getLength(s);
 scanf("%d", &n);
 for(int i=0; i<n; i++) {
  scanf("%s", s);
  fflush(stdin);
  int length = getLength(s);
  book[i].bookTitle = (char *)malloc(sizeof(char) * length);
  strcpy(book[i].bookTitle, s);
  scanf("%s", s);
  fflush(stdin);
  length = getLength(s);
  book[i].bookAuthor = (char *)malloc(sizeof(char) * length);
  strcpy(book[i].bookAuthor, s);
  scanf("%s", s);
  length = getLength(s);
  fflush(stdin);
  book[i].subject = (char *)malloc(sizeof(char) * length);
  strcpy(book[i].subject, s);
  scanf("%ld", &book[i].isbn);
  if(isValidISBN(book[i].isbn)==true) {
   printf("{Book Title : %s; ", book[i].bookTitle);
   printf("Book Author : %s; ", book[i].bookAuthor);
   printf("Subject : %s; ", book[i].subject);
   printf("ISBN : %ld}\n", book[i].isbn);
  }
 }
}

int getLength(char *s) {
 int length = 0;
 while(s[length]!='\0') {
  length++;
 }
 return length;
}

_Bool isValidISBN(long isbn) {
 long sum = 0;
 int i = 1;
 while(isbn>0) {
  sum = sum + (isbn)%10 * i;
  isbn = isbn / 10;
  i++;
 }
 if(sum%11==0)
  return true;
 return false;
}

No comments :

Post a Comment