给定长度为N 的字符串str ,任务是从此字符串删除大写、小写、特殊、数字和非数字字符,并在同时修改后打印该字符串。
例子:
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
朴素的方法:最简单的方法是遍历字符串并删除大写、小写、特殊、数字和非数字字符。以下是步骤:
1.遍历由字符字符串字符从开始到结束。
2. 检查每个字符的 ASCII 值是否符合以下条件:
- 如果 ASCII 值在[65, 90]范围内,则它是大写字符。因此,跳过这些字符并将其余字符添加到另一个字符串并打印出来。
- 如果 ASCII 值在[97, 122]范围内,则它是小写字符。因此,跳过这些字符并将其余字符添加到另一个字符串并打印出来。
- 如果 ASCII 值在[32, 47] 、 [58, 64] 、 [91, 96]或[123, 126] 的范围内,则它是特殊字符。因此,跳过这些字符并将其余字符添加到另一个字符串并打印出来。
- 如果 ASCII 值在[48, 57]范围内,则它是一个数字字符。因此,跳过这些字符并将其余字符添加到另一个字符串并打印出来。
- 否则字符是非数字字符。因此,跳过这些字符并将其余字符添加到另一个字符串并打印出来。
时间复杂度: O(N)
辅助空间: O(1)
正则表达式方法:这个想法是使用正则表达式来解决这个问题。以下是步骤:
1. 创建正则表达式以从字符串中删除大写、小写、特殊、数字和非数字字符,如下所述:
- regexToRemoveUpperCaseCharacters = “[A-Z]”
- regexToRemoveLowerCaseCharacters = “[a-z]”
- regexToRemoveSpecialCharacters = “[^A-Za-z0-9]”
- regexToRemoveNumericCharacters = “[0-9]”
- regexToRemoveNon-NumericCharacters = “[^0-9]”
2. 使用 Pattern.compile() 方法编译给定的正则表达式以创建模式。
3. 使用 Pattern.matcher() 将给定的字符串与上述所有正则表达式匹配。
4. 使用 Matcher.replaceAll() 方法用目标字符串替换每个匹配的模式。
下面是上述方法的实现:
C++
// C++ program to remove uppercase, lowercase
// special, numeric, and non-numeric characters
#include
#include
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
// 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
# 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
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
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live