Computer Science, asked by Sachinsachii, 5 hours ago

wap to remove alphanumeric characters from a string in java using scanner.​

Answers

Answered by ankitsingh99162
0

Method 1: Using ASCII values

Since the alphanumeric characters lie in the ASCII value range of [65, 90] for uppercase alphabets, [97, 122] for lowercase alphabets, and [48, 57] for digits. Hence traverse the string character by character and fetch the ASCII value of each character. If the ASCII value is not in the above three ranges, then the character is a non-alphanumeric character. Therefore skip such characters and add the rest in another string and print it.

Method 2: Using String.replaceAll()

Non-alphanumeric characters comprise of all the characters except alphabets and numbers. It can be punctuation characters like exclamation mark(!), at symbol(@), commas(, ), question mark(?), colon(:), dash(-) etc and special characters like dollar sign($), equal symbol(=), plus sign(+), apostrophes(‘).

The approach is to use the String.replaceAll method to replace all the non-alphanumeric characters with an empty string.

Above is the implementation of the above approach:

Attachments:
Similar questions