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.
- Check if a number is in given base or not
- C Program to check Armstrong Number
- C++ Program to check Prime Number
- Lex program to check whether input number is odd or even
- C Program to Check Whether a Number is Prime or not
- C program to Check Whether a Number is Positive or Negative or Zero
- C Program to Check if all digits of a number divide it
- Program to check for a Valid IMEI Number
- C/C++ Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Count of primes after converting given binary number in base between L to R
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Number of common base strings for two strings
- Check if a large number is divisible by a number which is a power of 2
- Program to check for ISBN
- Program to check if two strings are same or not
- Program to check the validity of a Password
- C++ program to check whether a String is a Pangram or not
- Program to check if all characters have even frequency
- A Program to check if strings are rotations of each other or not
- C Program to Check if count of divisors is even or odd
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.