Code:
while(index < 20)
{
input[index] = '\0';
index++;
}
Why? Setting input[index] = '\0' may be fine, but there's no need to fill the entire rest of the string.
Code:
weight = 10;
noOfChars = 0;
index = 0;
sum = 0;
flag = 0;
Stick these at the start of the code.
Code:
if((input[index] >= 0x30 && input[index] <= 0x39) ||
(input[index] == 'X' || input[index] == 'x'))
is easier to read (and write) as:
Code:
if(isdigit(input[index]) || tolower(input[index]) == 'x')
Similarly, use:
Code:
if(tolower(input[index]) == 'x')
I don't really see the point of your "flag" variable. Surely if there's no newline because the input is too long to fit in 20 chars, it will fail due to "not the right number of digits".
Code:
temp = temp * weight;
// why not
temp *= weight
--
Mats