檢查字串是否表示十六進位制數


在計算機科學中,十六進位制是一種以 16 為基數的數字系統。它使用 16 個不同的符號,包括從 0 到 9 的十個十進位制數字以及六個字母 A、B、C、D、E 和 F 來表示從 0 到 15 的數字。在本文中,我們將討論如何檢查字串是否表示十六進位制數。

問題陳述

給定一個字串,任務是檢查它是否表示有效的十六進位制數。

方法

我們可以透過迭代字串中的字元並檢查它們是否屬於有效十六進位制字元集來解決此問題。有效的十六進位制字元是從 0 到 9 的數字以及從 A 到 F 的字母(大寫或小寫)。如果字串中的所有字元都屬於此集合,則該字串表示有效的十六進位制數。

示例

以下是上述方法的程式 -

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

bool isHexadecimal(char *s) {
   int n = strlen(s);

   for (int i = 0; i < n; i++) {
      if (!isxdigit(s[i])) { // Check if the character is a valid hexadecimal digit
         return false;
      }
   }

   return true;
}

int main() {
   char s1[] = "ABCD1234";
   char s2[] = "12G4F5";

   if (isHexadecimal(s1)) {
      printf("%s represents a valid hexadecimal number.\n", s1);
   } else {
      printf("%s does not represent a valid hexadecimal number.\n", s1);
   }

   if (isHexadecimal(s2)) {
      printf("%s represents a valid hexadecimal number.\n", s2);
   } else {
      printf("%s does not represent a valid hexadecimal number.\n", s2);
   }

   return 0;
}

輸出

ABCD1234 represents a valid hexadecimal number.
12G4F5 does not represent a valid hexadecimal number.
#include <iostream>
#include <string>

using namespace std;

bool isHexadecimal(string s) {
   int n = s.length();
   
   for (int i = 0; i < n; i++) {
      if (!isxdigit(s[i])) {
         return false;
      }
   }
   
   return true;
}

int main() {
   string s1 = "ABCD1234";
   string s2 = "12G4F5";
   
   if (isHexadecimal(s1)) {
      cout << s1 << " represents a valid hexadecimal number." << endl;
   } else {
      cout << s1 << " does not represent a valid hexadecimal number." << endl;
   }
   
   if (isHexadecimal(s2)) {
      cout << s2 << " represents a valid hexadecimal number." << endl;
   } else {
      cout << s2 << " does not represent a valid hexadecimal number." << endl;
   }
   
   return 0;
}

輸出

ABCD1234 represents a valid hexadecimal number.
12G4F5 does not represent a valid hexadecimal number.
public class HexadecimalCheck {
   public static boolean isHexadecimal(String s) {
      int n = s.length();

      for (int i = 0; i < n; i++) {
         if (!Character.isDigit(s.charAt(i)) && !Character.isLetter(s.charAt(i))) { // Check if the character is a valid hexadecimal digit
               return false;
          }
      }

      return true;
   }

   public static void main(String[] args) {
      String s1 = "ABCD1234";
      String s2 = "12G4F5";

      if (isHexadecimal(s1)) {
            System.out.println(s1 + " represents a valid hexadecimal number.");
      } else {
            System.out.println(s1 + " does not represent a valid hexadecimal number.");
      }

      if (isHexadecimal(s2)) {
            System.out.println(s2 + " represents a valid hexadecimal number.");
      } else {
            System.out.println(s2 + " does not represent a valid hexadecimal number.");
      }
   }
}

輸出

ABCD1234 represents a valid hexadecimal number.
12G4F5 does not represent a valid hexadecimal number.
def is_hexadecimal(s):
   for char in s:
      if not char.isalnum(): # Check if the character is a valid hexadecimal digit
         return False
   return True

s1 = "ABCD1234"
s2 = "12G4F5"

if is_hexadecimal(s1):
   print(f"{s1} represents a valid hexadecimal number.")
else:
   print(f"{s1} does not represent a valid hexadecimal number.")

if is_hexadecimal(s2):
   print(f"{s2} represents a valid hexadecimal number.")
else:
   print(f"{s2} does not represent a valid hexadecimal number.")

輸出

ABCD1234 represents a valid hexadecimal number.
12G4F5 does not represent a valid hexadecimal number.

時間複雜度

解決方案的時間複雜度為 O(N),其中 N 是字串的長度。

空間複雜度

解決方案的空間複雜度為 O(1)。

在上面的程式碼中,我們定義了一個名為 isHexadecimal 的函式,它接受一個字串作為輸入,如果字串表示有效的十六進位制數則返回 true,否則返回 false。我們使用了 isxdigit 函式來檢查字串中的每個字元是否屬於有效十六進位制字元集。

測試用例

讓我們取兩個字串 s1 = "ABCD1234" 和 s2 = "12G4F5"。字串 s1 表示有效的十六進位制數,因為字串中的所有字元都屬於有效十六進位制字元集。另一方面,字串 s2 不表示有效的十六進位制數,因為它包含字元 'G',它不是有效的十六進位制字元。

結論

總之,我們可以透過迭代字串的字元並檢查它們是否屬於有效十六進位制字元集來輕鬆檢查字串是否表示有效的十六進位制數。

更新於: 2023-10-16

3K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告