Removing Non-alphanumeric characters from a string
In most cases, we come across some strings having non-alphanumeric characters that need to be converted into Alphanumeric strings because of limitations of devices in processing Non-alphanumeric characters.
Now let’s look at some of the methods to do that:
Using ASCII values
Because alphanumeric characters have ASCII values of [65, 90] for uppercase alphabets, [97, 122] for lowercase alphabets, and [48, 57] for digits. As a result, go through the string character by character and get the ASCII value of each character. If the ASCII value is not in one of the above three ranges, the character is Non-alphanumeric. As a result, skip such characters and add the rest to another string before printing it.
Implementation in c++:
#include <bits/stdc++.h>
using namespace std;
int main(){
string str = "Coding #*( $ Ninj@$";
string ans = "";
for(int i=0;i<str.size();i++){
int ASCII = (int)str[i];
if(ASCII>=48 && ASCII<=57){ //for digits
ans += str[i];
}
else if(ASCII>=65 && ASCII<=90){ //for uppercase
ans += str[i];
}
else if(ASCII>=97 && ASCII<=122){ //for lowercase
ans += str[i];
}
else continue;
}
cout<<ans;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
CodingNinj
You can also try this code with Online C++ Compiler
Run Code
Using in-built functions:
Programming languages have several builtin functions to check for alphanumeric characters
Implementation in c++:
#include <bits.stdc++.h>
using namespace std;
int main(){
string str = "Coding #*( $ Ninj@$";
string ans = "";
for(int i=0;i<str.size();i++){
// check for alphanumeric characters
if(isalnum(str[i])){
ans += str[i];
}
else continue;
}
cout<<ans;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
CodingNinj
You can also try this code with Online C++ Compiler
Run Code
You can try and compile with the help of online c++ compiler.
Using Regular Expression
Since the required string contains only characters from 1-9, a-z and A-Z so we can filter the ans string using regex [^a-zA-Z0-9]
Implementation in Java:
class Coding_ninjas{
public static void main(String args[])
{
String str1 = "Coding #*( $ Ninj@$";
//replace all characters other than alphanumeric
str1 = str1.replaceAll("[^a-zA-Z0-9]", "");
System.out.println(str1);
}
}
You can also try this code with Online Java Compiler
Run Code
Output:
CodingNinj
You can also try this code with Online Java Compiler
Run Code
Fibonacci Series in C++
Non-alphanumeric Characters in Passwords
Non-alphanumeric characters make good passwords because they use symbols and punctuation, which are difficult to crack if someone tries to hack your account. It is for their enhanced security, which is mostly used in our social media accounts, as indicated by comments from people who have used it previously. They use numerals, Arabic signs, upper case letters, numbers, and lower case letters to perfectly secure your account. As a result, people nowadays prefer to use passwords on computers that contain non-alphanumeric characters.
Most sites require passwords to have at least 8-12 characters and to be unique as well as strong. Passwords that are formed in a different and unique manner are considered strong, or else the site will tell you to change them as soon as possible, and there are various tools available nowadays to create unique passwords. Hackers have become experts at cracking passwords and stealing personal information, so it is critical to create strong passwords yourself or with the assistance of a strong password-creation tool.
Frequently Asked Questions
What are Non-alphanumeric Characters?
Non-alphanumeric characters are characters other than letters and numbers, i.e., all the characters on the keyboard which are not letters and numbers are Nonalphamuneric characters.
Why are Non-alphanumeric Characters important?
These characters are used for creating strong passwords, and for various important sites, it is a must to have at least one such character. Also, these are used for denoting various scientific terms.
What are ASCII values?
ASCII values are just a character encoding scheme used for electronic communication, each character is represented by a fixed ASCII value. Its full form is American Standard Code for information interchange.
Conclusion
In this article, we discussed Non-alphanumeric characters and their examples. We looked for different methods of removing non-alphanumeric characters from the string and the main use of non-alphanumeric characters as passwords for different websites.
If you think this blog has helped you enhance your knowledge about the above question, and if you would like to learn more, check out our articles.
And many more on our website.
Visit our website to read more such blogs. Make sure you enroll in the courses we provide, take mock tests, solve problems, and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations.