📌  相关文章
📜  检查字符串是否仅包含特殊字符

📅  最后修改于: 2021-04-29 04:17:39             🧑  作者: Mango

给定的字符串str的长度为N ,任务是检查给定的字符串是否仅包含特殊字符。如果字符串仅包含特殊字符,则打印“是” 。否则,打印“否”

例子:

天真的方法:遍历字符串并检查字符串是否仅包含特殊字符。请按照以下步骤解决问题:

  • 遍历字符串,对于每个字符,检查其ASCII值是否在[32,47][58,64][91,96][123,126]范围内。如果发现是正确的,则它是一个特殊字符。
  • 如果所有字符都位于上述范围之一,则打印“是” 。否则,打印编号。

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

节省空间的方法:想法是使用正则表达式来优化上述方法。请按照以下步骤操作:

  • 创建以下正则表达式以检查给定的字符串是否仅包含特殊字符。
  • 使用Pattern.matcher()将给定的字符串与正则表达式匹配 在Java
  • 如果字符串以给定的正则表达式匹配打印。否则,打印No。

下面是上述方法的实现:

C++
// C++ program to check if the string
// contains only special characters
#include 
#include 
using namespace std;
 
// Function to check if the string contains only special characters.
void onlySpecialCharacters(string str)
{
 
  // Regex to check if the string contains only special characters.
  const regex pattern("[^a-zA-Z0-9]+");
 
 
  // If the string
  // is empty then print "No"
  if (str.empty())
  {
     cout<< "No";
     return ;
  }
 
  // Print "Yes" if the the string contains only special characters
  // matched the ReGex
  if(regex_match(str, pattern))
  {
    cout<< "Yes";
  }
  else
  {
    cout<< "No";
  }
}
 
// Driver Code
int main()
{
   
  // Given string str
  string str = "@#$&%!~";
  onlySpecialCharacters(str) ;
 
  return 0;
}
 
// This code is contributed by yuvraj_chandra


Java
// Java program to check if the string
// contains only special characters
 
import java.util.regex.*;
class GFG {
 
    // Function to check if a string
    // contains only special characters
    public static void onlySpecialCharacters(
        String str)
    {
 
        // Regex to check if a string contains
        // only special characters
        String regex = "[^a-zA-Z0-9]+";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the string is empty
        // then print No
        if (str == null) {
            System.out.println("No");
            return;
        }
 
        // Find match between given string
        // & regular expression
        Matcher m = p.matcher(str);
 
        // Print Yes If the string matches
        // with the Regex
        if (m.matches())
            System.out.println("Yes");
        else
            System.out.println("No");
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Given string str
        String str = "@#$&%!~";
 
        // Function Call
        onlySpecialCharacters(str);
    }
}


Python3
# Python program to check if the string
# contains only special characters
import re
 
# Function to check if a string
# contains only special characters
def onlySpecialCharacters(Str):
 
    # Regex to check if a string contains
    # only special characters
    regex = "[^a-zA-Z0-9]+"
 
    # Compile the ReGex
    p=re.compile(regex)
 
    # If the string is empty
    # then print No
    if(len(Str) == 0):
        print("No")
        return
 
    # Print Yes If the string matches
    # with the Regex
    if(re.search(p, Str)):
        print("Yes")
    else:
        print("No")
 
# Driver Code
 
# Given string str
Str = "@#$&%!~"
 
# Function Call
onlySpecialCharacters(Str)
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program to check if
// the string contains only
// special characters
using System;
using System.Text.RegularExpressions; 
class GFG{
 
// Function to check if a string
// contains only special characters
public static void onlySpecialchars(String str)
{
  // Regex to check if a string
  // contains only special
  // characters
  String regex = "[^a-zA-Z0-9]+";
 
  // Compile the ReGex
  Regex rgex = new Regex(regex); 
 
  // If the string is empty
  // then print No
  if (str == null)
  {
    Console.WriteLine("No");
    return;
  }
 
  // Find match between given
  // string & regular expression
  MatchCollection matchedAuthors =
                  rgex.Matches(str);   
 
  // Print Yes If the string matches
  // with the Regex
  if (matchedAuthors.Count != 0)
    Console.WriteLine("Yes");
  else
    Console.WriteLine("No");
}
 
// Driver Code
public static void Main(String []args)
{
  // Given string str
  String str = "@#$&%!~";
 
  // Function Call
  onlySpecialchars(str);
}
}
 
// This code is contributed by Princi Singh


输出:
Yes

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