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 = 783740Input: 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.
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 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
// Java implementation of the above approachimport 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 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#
// 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 |
Javascript
<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> |
Output:
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)


