Examples:
Input: str = “Hello, how are you ?” Output: 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 ?” Output: Azad is a good boy isn t he
Approach:
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); } }
Output:
Hello how are you
Time Complexity: O(N)
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.