📌  相关文章
📜  检查给定字符串是否是有效的十六进制颜色代码

📅  最后修改于: 2022-05-13 01:56:08.334000             🧑  作者: Mango

检查给定字符串是否是有效的十六进制颜色代码

给定一个字符串str ,任务是检查给定字符串是否是 HTML Hex Color Code。如果是则打印Yes ,否则打印No

例子:

方法: HTML 十六进制颜色代码遵循下面提到的一组规则:

  1. 它以“#”符号开头。
  2. 然后是afAF的字母和/或0-9的数字。
  3. 十六进制颜色代码的长度应为 6 或 3,不包括“#”符号。
  4. 例如:#abc、#ABC、#000、#FFF、#000000、#FF0000、#00FF00、#0000FF、#FFFFFF 都是有效的十六进制颜色代码。

现在,要解决上述问题,请按照以下步骤操作:

  1. 检查字符串str是否满足以下条件:
    • 如果第一个字符不是# ,则返回false
    • 如果长度不是36 。如果不是,则返回false
    • 现在,检查除第一个字符之外的所有字符,即0-9AFaf
  2. 如果满足上述所有条件,则返回true
  3. 根据以上观察打印答案。

下面是上述方法的实现:

CPP
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to validate
// the HTML hexadecimal color code.
bool isValidHexaCode(string str)
{
    if (str[0] != '#')
        return false;
 
    if (!(str.length() == 4 or str.length() == 7))
        return false;
 
    for (int i = 1; i < str.length(); i++)
        if (!((str[i] >= '0' && str[i] <= 9)
              || (str[i] >= 'a' && str[i] <= 'f')
              || (str[i] >= 'A' || str[i] <= 'F')))
            return false;
 
    return true;
}
 
// Driver Code
int main()
{
    string str = "#1AFFa1";
 
    if (isValidHexaCode(str)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
    // Function to validate
    // the HTML hexadecimal color code.
    static boolean isValidHexaCode(String str)
    {
        if (str.charAt(0) != '#')
            return false;
 
        if (!(str.length() == 4 || str.length() == 7))
            return false;
 
        for (int i = 1; i < str.length(); i++)
            if (!((str.charAt(i) >= '0' && str.charAt(i) <= 9)
                || (str.charAt(i) >= 'a' && str.charAt(i) <= 'f')
                || (str.charAt(i) >= 'A' || str.charAt(i) <= 'F')))
                return false;
 
        return true;
    }
     
    // Driver code
    public static void main(String args[])
    {
        String str = "#1AFFa1";
 
        if (isValidHexaCode(str)) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# python program for the above approach
 
# Function to validate
# the HTML hexadecimal color code.
def isValidHexaCode(str):
 
    if (str[0] != '#'):
        return False
 
    if (not(len(str) == 4 or len(str) == 7)):
        return False
 
    for i in range(1, len(str)):
        if (not((str[i] >= '0' and str[i] <= '9') or (str[i] >= 'a' and str[i] <= 'f') or (str[i] >= 'A' or str[i] <= 'F'))):
            return False
 
    return True
 
 
# Driver Code
if __name__ == "__main__":
 
    str = "#1AFFa1"
 
    if (isValidHexaCode(str)):
        print("Yes")
 
    else:
        print("No")
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG{
 
    // Function to validate
    // the HTML hexadecimal color code.
    static bool isValidHexaCode(string str)
    {
        if (str[0] != '#')
            return false;
 
        if (!(str.Length == 4 || str.Length == 7))
            return false;
 
        for (int i = 1; i < str.Length; i++)
            if (!((str[i] >= '0' && str[i] <= 9)
                || (str[i] >= 'a' && str[i] <= 'f')
                || (str[i] >= 'A' || str[i] <= 'F')))
                return false;
 
        return true;
    }
     
    // Driver code
    public static void Main()
    {
        string str = "#1AFFa1";
 
        if (isValidHexaCode(str)) {
            Console.Write("Yes");
        }
        else {
            Console.Write("No");
        }
    }
}
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
Yes

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