Check if a string represents a hexadecimal number or not

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:



  1. Iterate over the given string.
  2. Check if each character of the string is between characters A to F or between 0 and 9.
  3. If found to be true, then print Yes.
  4. Otherwise, print No.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the string
// represents a hexadecimal number
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'))
        {
            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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the
# above approach
 
# Function to check if the string
# represents a hexadecimal number
def checkHex(s):
        
    # Size of string
    n = len(s)
  
    # Iterate over string
    for i in range(n):
        ch = s[i]
  
        # 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 string
s = "BF57C"
  
# Function call
checkHex(s)
 
# This code is contributed by sanjoy_62

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of
// the above approach
using System;
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[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 Code
public static void Main(String[] args)
{
  // Given string
  String s = "BF57C";
 
  // Function Call
  checkHex(s);
}
}
 
// This code is contributed by gauravrajput1

chevron_right


Output: 

Yes


Time Complexity: O(N) 
Auxiliary Space: O(1)

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.