Given a string str, consisting of non-alphabetical characters. The task is to remove all those non-alphabetical characters of str and print the words on a new line.
Input: str = “Hello, how are you ?”
comma(, ), white space and question mark (?) are removed and there are total 4 words in string s.
Each token is printed in the same order in which it appears in string s.
Input: “Azad is a good boy, isn’ t he ?”
Approach: Non-alphabetic characters are basically any character that is not a number or letter. It can be English alphabetic letters, blank spaces, exclamation points (!), commas (, ), question marks (?), periods (.), underscores (_), apostrophes (‘), and at symbols (@). The approach is to use Java String.split method to split the String, s into an array of substrings. Then print each n words on a new line in the same order as it appears in String s.
Below is the implementation of the above approach:
Hello how are you
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How to remove all non-alphanumeric characters from a string in Java
- Remove characters from the first string which are present in the second string
- Remove even frequency characters from the string
- Remove odd frequency characters from the string
- Remove all characters other than alphabets from string
- Remove characters from a String that appears exactly K times
- Remove characters from string that appears strictly less than K times
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Remove minimum characters from string to split it into three substrings under given constraints
- Remove first and last character of a string in Java
- Swapping Characters of a String in Java
- Remove Leading Zeros From String in Java
- How to remove all white spaces from a String in Java?
- Remove extra delimiter from a String in Java
- Searching characters and substring in a String in Java
- Convert a String to a List of Characters in Java
- Convert List of Characters to String in Java
- Java program to print all duplicate characters in a string
- Trim (Remove leading and trailing spaces) a string in Java
- Count of ungrouped characters after dividing a string into K groups of distinct characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.