📜  检查字符串代表十六进制数字

📅  最后修改于: 2021-04-22 01:42:42             🧑  作者: Mango

给定长度为N的字母数字字符串S ,任务是检查给定的字符串表示一个十六进制数。如果它代表一个十六进制数字,则打印。否则,打印No。

例子:

方法:该方法基于以下思想:十六进制数字的所有元素都位于字符A至F之间或整数0至9之间。以下是解决问题的步骤:

  1. 遍历给定的字符串。
  2. 检查字符串的每个字符是在字符A到F之间还是在0到9之间。
  3. 如果发现是真的,则打印“是”
  4. 否则,打印No。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
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


输出:
Yes

时间复杂度: O(N)
辅助空间: O(1)