Given string str of length N, the task is to remove uppercase, lowercase, special, numeric, and non-numeric characters from this string and print the string after the simultaneous modifications.
Examples:
Input: str = “GFGgfg123$%”
Output: After removing uppercase characters: gfg123$%
After removing lowercase characters: GFG123$%
After removing special characters: GFGgfg123
After removing numeric characters: GFGgfg$%
After removing non-numeric characters: 123
Input: str = “J@va12”
Output: After removing uppercase characters: @va12
After removing lowercase characters: J@12
After removing special characters: Jva12
After removing numeric characters: J@va
After removing non-numeric characters: 12
Naive Approach: The simplest approach is to iterate over the string and remove uppercase, lowercase, special, numeric, and non-numeric characters. 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 character. Therefore, skip such characters and add the rest characters in another string and print it.
- If the ASCII value lies in the range of [97, 122], then it is a lowercase character. Therefore, skip such characters and add the rest characters in another string and print it.
- If the ASCII value lies in the range of [32, 47], [58, 64], [91, 96], or [123, 126] then it is a special character. Therefore, skip such characters and add the rest characters in another string and print it.
- If the ASCII value lies in the range of [48, 57], then it is a numeric character. Therefore, skip such characters and add the rest characters in another string and print it.
- Else the character is a non-numeric character. Therefore, skip such characters and add the rest characters in another string and print it.
Time Complexity: O(N)
Auxiliary Space: O(1)
Regular Expression Approach: The idea is to use regular expressions to solve this problem. Below are the steps:
1. Create regular expressions to remove uppercase, lowercase, special, numeric, and non-numeric characters from the string as mentioned below:
- regexToRemoveUpperCaseCharacters = “[A-Z]”
- regexToRemoveLowerCaseCharacters = “[a-z]”
- regexToRemoveSpecialCharacters = “[^A-Za-z0-9]”
- regexToRemoveNumericCharacters = “[0-9]”
- regexToRemoveNon-NumericCharacters = “[^0-9]”
2. Compile the given regular expressions to create the pattern using Pattern.compile() method.
3. Match the given string with all the above Regular Expressions using Pattern.matcher().
4. Replace every matched pattern with the target string using the Matcher.replaceAll() method.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
string removingUpperCaseCharacters(string str)
{
const regex pattern( "[A-Z]" );
return regex_replace(str, pattern, "" );
}
string removingLowerCaseCharacters(string str)
{
const regex pattern( "[a-z]" );
return regex_replace(str, pattern, "" );
}
string removingSpecialCharacters(string str)
{
const regex pattern( "[^A-Za-z0-9]" );
return regex_replace(str, pattern, "" );
}
string removingNumericCharacters(string str)
{
const regex pattern( "[0-9]" );
return regex_replace(str, pattern, "" );
}
string removingNonNumericCharacters(string str)
{
const regex pattern( "[^0-9]" );
return regex_replace(str, pattern, "" );
}
int main()
{
string str = "GFGgfg123$%" ;
cout << "After removing uppercase characters: "
<< removingUpperCaseCharacters(str) << endl;
cout << "After removing lowercase characters: "
<< removingLowerCaseCharacters(str) << endl;
cout << "After removing special characters: "
<< removingSpecialCharacters(str) << endl;
cout << "After removing numeric characters: "
<< removingNumericCharacters(str) << endl;
cout << "After removing non-numeric characters: "
<< removingNonNumericCharacters(str) << endl;
return 0;
}
|
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GFG
{
public static String
removingUpperCaseCharacters(String str)
{
String regex = "[A-Z]" ;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.replaceAll( "" );
}
public static String
removingLowerCaseCharacters(String str)
{
String regex = "[a-z]" ;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.replaceAll( "" );
}
public static String
removingSpecialCharacters(String str)
{
String regex = "[^A-Za-z0-9]" ;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.replaceAll( "" );
}
public static String
removingNumericCharacters(String str)
{
String regex = "[0-9]" ;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.replaceAll( "" );
}
public static String
removingNonNumericCharacters(String str)
{
String regex = "[^0-9]" ;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.replaceAll( "" );
}
public static void main(String[] args)
{
String str = "GFGgfg123$%" ;
System.out.println(
"After removing uppercase characters: "
+ removingUpperCaseCharacters(str));
System.out.println(
"After removing lowercase characters: "
+ removingLowerCaseCharacters(str));
System.out.println(
"After removing special characters: "
+ removingSpecialCharacters(str));
System.out.println(
"After removing numeric characters: "
+ removingNumericCharacters(str));
System.out.println(
"After removing non-numeric characters: "
+ removingNonNumericCharacters(str));
}
}
|
Python3
import re
def removingUpperCaseCharacters( str ):
regex = "[A-Z]"
return (re.sub(regex, "", str ))
def removingLowerCaseCharacters( str ):
regex = "[a-z]"
return (re.sub(regex, "", str ))
def removingSpecialCharacters( str ):
regex = "[^A-Za-z0-9]"
return (re.sub(regex, "", str ))
def removingNumericCharacters( str ):
regex = "[0-9]"
return (re.sub(regex, "", str ))
def removingNonNumericCharacters( str ):
regex = "[^0-9]"
return (re.sub(regex, "", str ))
str = "GFGgfg123$%"
print ( "After removing uppercase characters:" ,
removingUpperCaseCharacters( str ))
print ( "After removing lowercase characters:" ,
removingLowerCaseCharacters( str ))
print ( "After removing special characters:" ,
removingSpecialCharacters( str ))
print ( "After removing numeric characters:" ,
removingNumericCharacters( str ))
print ( "After removing non-numeric characters:" ,
removingNonNumericCharacters( str ))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG
{
public static string
removingUpperCaseCharacters( string str)
{
string regex = "[A-Z]" ;
return Regex.Replace(str, regex, "" );
}
public static string
removingLowerCaseCharacters( string str)
{
string regex = "[a-z]" ;
return Regex.Replace(str, regex, "" );
}
public static string
removingSpecialCharacters( string str)
{
string regex = "[^A-Za-z0-9]" ;
return Regex.Replace(str, regex, "" );
}
public static string
removingNumericCharacters( string str)
{
string regex = "[0-9]" ;
return Regex.Replace(str, regex, "" );
}
public static string
removingNonNumericCharacters( string str)
{
string regex = "[^0-9]" ;
return Regex.Replace(str, regex, "" );
}
public static void Main()
{
string str = "GFGgfg123$%" ;
Console.WriteLine(
"After removing uppercase characters: "
+ removingUpperCaseCharacters(str));
Console.WriteLine(
"After removing lowercase characters: "
+ removingLowerCaseCharacters(str));
Console.WriteLine(
"After removing special characters: "
+ removingSpecialCharacters(str));
Console.WriteLine(
"After removing numeric characters: "
+ removingNumericCharacters(str));
Console.WriteLine(
"After removing non-numeric characters: "
+ removingNonNumericCharacters(str));
}
}
|
Javascript
function removingUpperCaseCharacters(str)
{
let pattern = new RegExp( "[A-Z]" , 'g' );
return str.replace(pattern, "" );
}
function removingLowerCaseCharacters(str)
{
let pattern = new RegExp( "[a-z]" , 'g' );
return str.replace(pattern, "" );
}
function removingSpecialCharacters(str)
{
let pattern = new RegExp( "[^A-Za-z0-9]" , 'g' );
return str.replace(pattern, "" );
}
function removingNumericCharacters(str)
{
let pattern = new RegExp( "[0-9]" , 'g' );
return str.replace(pattern, "" );
}
function removingNonNumericCharacters(str)
{
let pattern = new RegExp( "[^0-9]" , 'g' );
return str.replace(pattern, "" );
}
let str = "GFGgfg123$%" ;
console.log( "After removing uppercase characters: "
+ removingUpperCaseCharacters(str)+ "<br>" );
console.log( "After removing lowercase characters: "
+ removingLowerCaseCharacters(str)+ "<br>" );
console.log( "After removing special characters: "
+ removingSpecialCharacters(str)+ "<br>" );
console.log( "After removing numeric characters: "
+ removingNumericCharacters(str)+ "<br>" );
console.log( "After removing non-numeric characters: "
+ removingNonNumericCharacters(str)+ "<br>" );
|
Output
After removing uppercase characters: gfg123$%
After removing lowercase characters: GFG123$%
After removing special characters: GFGgfg123
After removing numeric characters: GFGgfg$%
After removing non-numeric characters: 123
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
03 Jan, 2023
Like Article
Save Article