Check if a string contains uppercase, lowercase, special characters and numeric values
Last Updated :
11 Nov, 2021
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 characters. Below are the steps:
- Traverse the string character by character from start to end.
- 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
- 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:
C++
#include <iostream>
#include <regex>
using namespace std;
void isAllPresent(string str)
{
const regex pattern( "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[-+_!@#$%^&*.,?]).+$" );
if (str.empty())
{
cout<< "No" <<endl;
return ;
}
if (regex_match(str, pattern))
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
return ;
}
int main()
{
string str = "#GeeksForGeeks123@" ;
isAllPresent(str);
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static void
isAllPresent(String str)
{
String regex = "^(?=.*[a-z])(?=."
+ "*[A-Z])(?=.*\\d)"
+ "(?=.*[-+_!@#$%^&*., ?]).+$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
System.out.println( "No" );
return ;
}
Matcher m = p.matcher(str);
if (m.matches())
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String args[])
{
String str = "#GeeksForGeeks123@" ;
isAllPresent(str);
}
}
|
Python3
import re
def isAllPresent( str ):
regex = ( "^(?=.*[a-z])(?=." +
"*[A-Z])(?=.*\\d)" +
"(?=.*[-+_!@#$%^&*., ?]).+$" )
p = re. compile (regex)
if ( str = = None ):
print ( "No" )
return
if (re.search(p, str )):
print ( "Yes" )
else :
print ( "No" )
str = "#GeeksForGeeks123@"
isAllPresent( str )
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
static void
isAllPresent( string str)
{
string regex = "^(?=.*[a-z])(?=."
+ "*[A-Z])(?=.*\\d)"
+ "(?=.*[-+_!@#$%^&*., ?]).+$" ;
Regex p = new Regex(regex);
if (str == null ) {
Console.Write( "No" );
return ;
}
Match m = p.Match(str);
if (m.Success)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main()
{
string str = "#GeeksForGeeks123@" ;
isAllPresent(str);
}
}
|
Javascript
<script>
function isAllPresent(str) {
var pattern = new RegExp(
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[-+_!@#$%^&*.,?]).+$"
);
if (!str || str.length === 0) {
document.write( "No" + "<br>" );
return ;
}
if (pattern.test(str)) {
document.write( "Yes" + "<br>" );
} else {
document.write( "No" + "<br>" );
}
return ;
}
var str = "#GeeksForGeeks123@" ;
isAllPresent(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...