Check if a string contains uppercase, lowercase, special characters and numeric values

Given string str of length N, the task is to check whether the given string contains uppercase alphabets, lowercase alphabets, special characters, and numeric values or not. If the string contains all of them, then print “Yes”. Otherwise, print “No”

Examples: 

Input: str = “#GeeksForGeeks123@” 
Output: Yes 
Explanation: 
The given string contains uppercase characters(‘G’, ‘F’), lowercase characters(‘e’, ‘k’, ‘s’, ‘o’, ‘r’), special characters( ‘#’, ‘@’), and numeric values(‘1’, ‘2’, ‘3’). Therefore, the output is Yes. 
 

Input: str = “GeeksForGeeks” 
Output: No 
Explanation: 
The given string contains only uppercase characters and lowercase characters. Therefore, the output is No.

Naive Approach: The simplest approach is to iterate over the string and check if the given string contains uppercase, lowercase, numeric and special character. Below are the steps: 



  1. Traverse the string character by character from start to end.
  2. Check the ASCII value of each character for the following conditions: 
    • If the ASCII value lies in the range of [65, 90], then it is an uppercase letter.
    • If the ASCII value lies in the range of [97, 122], then it is a lowercase letter.
    • If the ASCII value lies in the range of [48, 57], then it is a number.
    • If the ASCII value lies in the ranges [32, 47], [58, 64], [91, 96] or [123, 126], then it is a special character
  3. Print Yes if the string contains all the above. Otherwise, print No.

Time Complexity: O(N) 
Auxiliary Space: O(1)
 

Regular Expression Approach: The idea is to the concept of a regular expression to solve this problem. Below are the steps: 

  • Create a regular expression to check if the given string contains uppercase, lowercase, special character, and numeric values as mentioned below: 

regex = “^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)” + “(?=.*[-+_!@#$%^&*., ?]).+$” 
where, 
 

  • ^ represents the starting of the string.
  • (?=.*[a-z]) represent at least one lowercase character.
  • (?=.*[A-Z]) represents at least one uppercase character.
  • (?=.*\\d) represents at least one numeric value.
  • (?=.*[-+_!@#$%^&*., ?]) represents at least one special character.
  • . represents any character except line break.
  • + represents one or more times.
  • Match the given string with the Regular Expression using Pattern.matcher().
  • Print Yes if the string matches with the given regular expression. Otherwise, print No.

Below is the implementation of the above approach:
 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
 
import java.util.regex.*;
 
class GFG {
 
    // Function that checks if a string
    // contains uppercase, lowercase
    // special character & numeric value
    public static void
    isAllPresent(String str)
    {
        // ReGex to check if a string
        // contains uppercase, lowercase
        // special character & numeric value
        String regex = "^(?=.*[a-z])(?=."
                       + "*[A-Z])(?=.*\\d)"
                       + "(?=.*[-+_!@#$%^&*., ?]).+$";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the string is empty
        // then return false
        if (str == null) {
            System.out.println("No");
            return;
        }
 
        // Find match between given string
        // & regular expression
        Matcher m = p.matcher(str);
 
        // Print Yes if string
        // matches ReGex
        if (m.matches())
            System.out.println("Yes");
        else
            System.out.println("No");
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Given string
        String str = "#GeeksForGeeks123@";
 
        // Function Call
        isAllPresent(str);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the
# above approach
import re
 
# Function that checks if a string
# contains uppercase, lowercase
# special character & numeric value
def isAllPresent(str):
 
    # ReGex to check if a string
    # contains uppercase, lowercase
    # special character & numeric value
    regex = ("^(?=.*[a-z])(?=." +
             "*[A-Z])(?=.*\\d)" +
             "(?=.*[-+_!@#$%^&*., ?]).+$")
     
    # Compile the ReGex
    p = re.compile(regex)
 
    # If the string is empty
    # return false
    if (str == None):
        print("No")
        return
 
    # Print Yes if string
    # matches ReGex
    if(re.search(p, str)):
        print("Yes")
    else:
        print("No")
 
# Driver code
 
# Given string
str = "#GeeksForGeeks123@"
 
#Function Call
isAllPresent(str)
 
# This code is contributed by avanitrachhadiya2155

chevron_right


Output: 

Yes

 

Time Complexity: O(N) 
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

External Technical Content Reviewer at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : avanitrachhadiya2155