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.
Examples:
Input: str = “Hello, how are you ?”
Output:
Hello
how
are
youcomma(, ), 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 ?”
Output:
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:
// Java program to split all // non-alphabetical characters import java.util.Scanner; public class Main { // Function to trim the non-alphabetical characters static void printwords(String str) { // eliminate leading and trailing spaces str = str.trim(); // split all non-alphabetic characters String delims = "\\W+"; // split any non word String[] tokens = str.split(delims); // print the tokens for (String item : tokens) { System.out.println(item + " "); } } public static void main(String[] args) { String str = "Hello, how are you ?"; printwords(str); } } |
Hello how are you
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- How to remove all non-alphanumeric characters from a string in Java
- Remove all characters other than alphabets from string
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Remove all duplicate adjacent characters from a string using Stack
- Minimum number of operations to move all uppercase characters before all lower case characters
- Remove characters from the first string which are present in the second string
- Min flips of continuous characters to make all characters same in a string
- Remove even frequency characters from the string
- Remove characters from string that appears strictly less than K times
- Remove characters from a String that appears exactly K times
- Remove odd frequency characters from the string
- Remove minimum characters from string to split it into three substrings under given constraints
- Find value after N operations to remove N characters of string S with given constraints
- Remove odd indexed characters from a given string
- Replace minimal number of characters to make all characters pair wise distinct
- Minimum characters to be replaced to make frequency of all characters same
- String with k distinct characters and no same characters adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

