Check if a string represents a hexadecimal number or not - GeeksforGeeks

13 Apr, 2021
Given an alphanumeric string S of length N, the task is to check if the given string represents a hexadecimal number or not. Print Yes if it represents a hexadecimal number. Otherwise, print No.
Examples:
Input: S = “BF57C” Output: Yes Explanation: Decimal Representation of the given string = 783740
Input: S = “58GK” Output: No
Approach: The approach is based on the idea that all the elements of a hexadecimal number lie between characters A to F or between integers 0 to 9. Below are the steps to solve the problem:
Iterate over the given string.Check if each character of the string is between characters A to F or between 0 and 9.If found to be true, then print Yes.Otherwise, print No.
Iterate over the given string.
Check if each character of the string is between characters A to F or between 0 and 9.
If found to be true, then print Yes.
Otherwise, print No.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std; // Function to check if the string// represents a hexadecimal numbervoid checkHex(string s){         // Size of string    int n = s.length();     // Iterate over string    for(int i = 0; i < n; i++)    {        char ch = s[i];         // Check if the character        // is invalid        if ((ch < '0' || ch > '9') &&            (ch < 'A' || ch > 'F'))        {            cout << "No" << endl;            return;        }    }     // Print true if all    // characters are valid    cout << "Yes" << endl;} // Driver code    int main(){         // Given string    string s = "BF57C";     // Function call    checkHex(s);     return 0;} // This code is contributed by divyeshrabadiya07
// Java implementation of the above approach import java.util.*;import java.io.*; class GFG {     // Function to check if the string    // represents a hexadecimal number    public static void checkHex(String s)    {        // Size of string        int n = s.length();         // Iterate over string        for (int i = 0; i < n; i++) {             char ch = s.charAt(i);             // Check if the character            // is invalid            if ((ch < '0' || ch > '9')                && (ch < 'A' || ch > 'F')) {                 System.out.println("No");                return;            }        }         // Print true if all        // characters are valid        System.out.println("Yes");    }     // Driver Code    public static void main(String[] args)    {        // Given string        String s = "BF57C";         // Function Call        checkHex(s);    }}
# Python3 implementation of the# above approach # Function to check if the string# represents a hexadecimal numberdef checkHex(s):       # Iterate over string    for ch in s:         # Check if the character        # is invalid        if ((ch < '0' or ch > '9') and            (ch < 'A' or ch > 'F')):                             print("No")            return             # Print true if all    # characters are valid    print("Yes") # Driver Code     # Given strings = "BF57C"  # Function callcheckHex(s) # This code is contributed by extragornax
// C# implementation of// the above approachusing System;class GFG{ // Function to check if the string// represents a hexadecimal numberpublic static void checkHex(String s){  // Size of string  int n = s.Length;   // Iterate over string  for (int i = 0; i < n; i++)  {    char ch = s[i];     // Check if the character    // is invalid    if ((ch < '0' || ch > '9') &&        (ch < 'A' || ch > 'F'))    {      Console.WriteLine("No");      return;    }  }   // Print true if all  // characters are valid  Console.WriteLine("Yes");} // Driver Codepublic static void Main(String[] args){  // Given string  String s = "BF57C";   // Function Call  checkHex(s);}} // This code is contributed by gauravrajput1
<script> // JavaScript implementation of the// above approach // Function to check if the string// represents a hexadecimal numberfunction checkHex(s){         // Size of string    let n = s.length;     // Iterate over string    for(let i = 0; i < n; i++)    {        let ch = s[i];         // Check if the character        // is invalid        if ((ch < '0' || ch > '9') &&            (ch < 'A' || ch > 'F'))        {            document.write("No");            return;        }    }     // Print true if all    // characters are valid    document.write("Yes");} // Driver code // Given stringlet s = "BF57C"; // Function CallcheckHex(s); // This code is contributed by souravghosh0416 </script>
Yes
Time Complexity: O(N) Auxiliary Space: O(1)
GauravRajput1
divyeshrabadiya07
amit143katiyar
sanjoy_62
gaspard
souravghosh0416
number-digits
Numbers
Searching
Strings
Searching
Strings
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Best First Search (Informed Search)
3 Different ways to print Fibonacci series in Java
Find whether an array is subset of another array | Added Method 5
Given a sorted and rotated array, find if there is a pair with a given sum
Program to remove vowels from a String
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Longest Common Subsequence | DP-4