Wrap the code as a function:
int strtoisbn(char const *str)
{
int isbn = 0;
unsigned char c;
while ((c = *str++) != '\0')
{
if (isdigit(c))
isbn = isbn * 10 + (c - '0');
}
return isbn;
}
Then use it:
int isbn1 = strtoisbn("3-423-62167-2");
int isbn2 = strtoisbn("3-446-19313-8");
You need to worry about the use of int
as the data type (especially for 13-digit ISBNs); you might well need long long
or int64_t
or an unsigned variant of those. You also have to worry about the X
check digit for 10-digit ISBNs; these are not a factor in 13-digit ISBNs.
If you can't write functions yet, then (a) learn how to, and (b) reinitialize p
to point to the new ISBN:
p = ISBN2;
and then run the same code again. But writing the same code twice is an indication that you probably need to write a function to do the job.