Given a number N and its radix base R, find whether its valid or not according to its radix base.
Find valid/invalid number for any base ranging from binary to base32.
Input : 1000101010001 Output : Valid 1000101010001 is valid binary number. Input : 0xFFFAG Output : invalid 0xFFFAG is not a valid hexa-decimal number because of character 'G' .
Method used : strspn
- strspn :
strspn(numStr, validNumber) Strspan: returns number of matching digits from character-set provided, so here it will return 1 - N, where N is length of validNumber String. numStr : And that number is provided to numStr array.
- numStr :
numStr[strspn(numStr, validNumber)] Using this number as index for numStr array, we access the digit there and it will be NULL in case string is matched and it will be non-zero if string is not matched
- !numStr : And Finally we invert the result using invert operator to match for true case !
Binary string 1000101010001 is Valid Octal string 7778A is Invalid Decimal string 45221 is Valid Hex string FAG463 is Invalid Base32 string AD22F is Valid
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.