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++ program to remove uppercase, lowercase // special, numeric, and non-numeric characters #include <iostream> #include <regex> using namespace std;
// Function to remove uppercase characters string removingUpperCaseCharacters(string str) { // Create a regular expression
const regex pattern( "[A-Z]" );
// Replace every matched pattern with the
// target string using regex_replace() method
return regex_replace(str, pattern, "" );
} // Function to remove lowercase characters string removingLowerCaseCharacters(string str) { // Create a regular expression
const regex pattern( "[a-z]" );
// Replace every matched pattern with the
// target string using regex_replace() method
return regex_replace(str, pattern, "" );
} // Function to remove special characters string removingSpecialCharacters(string str) { // Create a regular expression
const regex pattern( "[^A-Za-z0-9]" );
// Replace every matched pattern with the
// target string using regex_replace() method
return regex_replace(str, pattern, "" );
} // Function to remove numeric characters string removingNumericCharacters(string str) { // Create a regular expression
const regex pattern( "[0-9]" );
// Replace every matched pattern with the
// target string using regex_replace() method
return regex_replace(str, pattern, "" );
} // Function to remove non-numeric characters string removingNonNumericCharacters(string str) { // Create a regular expression
const regex pattern( "[^0-9]" );
// Replace every matched pattern with the
// target string using regex_replace() method
return regex_replace(str, pattern, "" );
} int main()
{ // Given String str
string str = "GFGgfg123$%" ;
// Print the strings after the simultaneous
// modifications
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;
} // This article is contributed by yuvraj_chandra |
// Java program to remove uppercase, lowercase // special, numeric, and non-numeric characters import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GFG
{ // Function to remove uppercase characters
public static String
removingUpperCaseCharacters(String str)
{
// Create a regular expression
String regex = "[A-Z]" ;
// Compile the regex to create pattern
// using compile() method
Pattern pattern = Pattern.compile(regex);
// Get a matcher object from pattern
Matcher matcher = pattern.matcher(str);
// Replace every matched pattern with the
// target string using replaceAll() method
return matcher.replaceAll( "" );
}
// Function to remove lowercase characters
public static String
removingLowerCaseCharacters(String str)
{
// Create a regular expression
String regex = "[a-z]" ;
// Compile the regex to create pattern
// using compile() method
Pattern pattern = Pattern.compile(regex);
// Get a matcher object from pattern
Matcher matcher = pattern.matcher(str);
// Replace every matched pattern with the
// target string using replaceAll() method
return matcher.replaceAll( "" );
}
// Function to remove special characters
public static String
removingSpecialCharacters(String str)
{
// Create a regular expression
String regex = "[^A-Za-z0-9]" ;
// Compile the regex to create pattern
// using compile() method
Pattern pattern = Pattern.compile(regex);
// Get a matcher object from pattern
Matcher matcher = pattern.matcher(str);
// Replace every matched pattern with the
// target string using replaceAll() method
return matcher.replaceAll( "" );
}
// Function to remove numeric characters
public static String
removingNumericCharacters(String str)
{
// Create a regular expression
String regex = "[0-9]" ;
// Compile the regex to create pattern
// using compile() method
Pattern pattern = Pattern.compile(regex);
// Get a matcher object from pattern
Matcher matcher = pattern.matcher(str);
// Replace every matched pattern with the
// target string using replaceAll() method
return matcher.replaceAll( "" );
}
// Function to remove non-numeric characters
public static String
removingNonNumericCharacters(String str)
{
// Create a regular expression
String regex = "[^0-9]" ;
// Compile the regex to create pattern
// using compile() method
Pattern pattern = Pattern.compile(regex);
// Get a matcher object from pattern
Matcher matcher = pattern.matcher(str);
// Replace every matched pattern with the
// target string using replaceAll() method
return matcher.replaceAll( "" );
}
// Driver Code
public static void main(String[] args)
{
// Given String str
String str = "GFGgfg123$%" ;
// Print the strings after the simultaneous
// modifications
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 program to remove # uppercase, lowercase special, # numeric, and non-numeric characters import re
# Function to remove # uppercase characters def removingUpperCaseCharacters( str ):
# Create a regular expression
regex = "[A-Z]"
# Replace every matched pattern
# with the target string using
# sub() method
return (re.sub(regex, "", str ))
# Function to remove lowercase # characters def removingLowerCaseCharacters( str ):
# Create a regular expression
regex = "[a-z]"
# Replace every matched
# pattern with the target
# string using sub() method
return (re.sub(regex, "", str ))
def removingSpecialCharacters( str ):
# Create a regular expression
regex = "[^A-Za-z0-9]"
# Replace every matched pattern
# with the target string using
# sub() method
return (re.sub(regex, "", str ))
def removingNumericCharacters( str ):
# Create a regular expression
regex = "[0-9]"
# Replace every matched
# pattern with the target
# string using sub() method
return (re.sub(regex, "", str ))
def removingNonNumericCharacters( str ):
# Create a regular expression
regex = "[^0-9]"
# Replace every matched pattern
# with the target string using
# sub() method
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 ))
# This code is contributed by avanitrachhadiya2155 |
// C# program to remove uppercase, lowercase // special, numeric, and non-numeric characters using System;
using System.Text.RegularExpressions;
public class GFG
{ // Function to remove uppercase characters
public static string
removingUpperCaseCharacters( string str)
{
// Create a regular expression
string regex = "[A-Z]" ;
// Replace every matched pattern with the
// target string using Replace method
return Regex.Replace(str, regex, "" );
}
// Function to remove lowercase characters
public static string
removingLowerCaseCharacters( string str)
{
// Create a regular expression
string regex = "[a-z]" ;
// Replace every matched pattern with the
// target string using Replace method
return Regex.Replace(str, regex, "" );
}
// Function to remove special characters
public static string
removingSpecialCharacters( string str)
{
// Create a regular expression
string regex = "[^A-Za-z0-9]" ;
// Replace every matched pattern with the
// target string using Replace method
return Regex.Replace(str, regex, "" );
}
// Function to remove numeric characters
public static string
removingNumericCharacters( string str)
{
// Create a regular expression
string regex = "[0-9]" ;
// Replace every matched pattern with the
// target string using Replace method
return Regex.Replace(str, regex, "" );
}
// Function to remove non-numeric characters
public static string
removingNonNumericCharacters( string str)
{
// Create a regular expression
string regex = "[^0-9]" ;
// Replace every matched pattern with the
// target string using Replace method
return Regex.Replace(str, regex, "" );
}
// Driver Code
public static void Main()
{
// Given string str
string str = "GFGgfg123$%" ;
// Print the strings after the simultaneous
// modifications
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));
}
} // This code is contributed by Aman Kumar. |
// Javascript program to remove uppercase, lowercase // special, numeric, and non-numeric characters // Function to remove uppercase characters function removingUpperCaseCharacters(str)
{ // Create a regular expression let pattern = new RegExp( "[A-Z]" , 'g' );
// Replace every matched pattern with the // target string using str.replace() method return str.replace(pattern, "" );
} // Function to remove lowercase characters function removingLowerCaseCharacters(str)
{ // Create a regular expression let pattern = new RegExp( "[a-z]" , 'g' );
// Replace every matched pattern with the // target string using str.replace() method return str.replace(pattern, "" );
} // Function to remove special characters function removingSpecialCharacters(str)
{ // Create a regular expression let pattern = new RegExp( "[^A-Za-z0-9]" , 'g' );
// Replace every matched pattern with the // target string using str.replace() method return str.replace(pattern, "" );
} // Function to remove numeric characters function removingNumericCharacters(str)
{ // Create a regular expression let pattern = new RegExp( "[0-9]" , 'g' );
// Replace every matched pattern with the // target string using str.replace() method return str.replace(pattern, "" );
} // Function to remove non-numeric characters function removingNonNumericCharacters(str)
{ // Create a regular expression let pattern = new RegExp( "[^0-9]" , 'g' );
// Replace every matched pattern with the // target string using str.replace() method return str.replace(pattern, "" );
} // Given String str let str = "GFGgfg123$%" ;
// Print the strings after the simultaneous // modifications 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>" );
// This article is contributed by Pushpesh Raj |
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)