Skip to content
Related Articles

Related Articles

Improve Article

Check if a string represents a hexadecimal number or not

  • Last Updated : 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:



  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++




// 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

Java




// 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




# Python3 implementation of the
# above approach
 
# Function to check if the string
# represents a hexadecimal number
def 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 string
s = "BF57C"
  
# Function call
checkHex(s)
 
# This code is contributed by extragornax

C#




// 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

Javascript




<script>
 
// JavaScript implementation of the
// above approach
 
// Function to check if the string
// represents a hexadecimal number
function 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 string
let s = "BF57C";
 
// Function Call
checkHex(s);
 
// This code is contributed by souravghosh0416
 
</script>
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :