Program to print the word in newline whenever the “whitespace” is encountered in a given string in Java (2 Methods)

Description


Description

EXAMPLE

Entered string->“Welcome to programmingguide.in”

Output->

Welcome

           to

           programmingguide.in

Here string is entered in the variable ‘str’ of type String by user.

Now we execute a loop in which the variable ‘i’ is initialized with the value zero and the looping is continued until its value is less than string length.

We use charAt() funtion to access the characters of string with specified index number.

In the loop,

 if encountered character is whitespace then the control jumps to newline

 else the character is printed.

Description

Program to print the word in newline whenever the whitespace is encountered in a given string.

EXAMPLE

Entered string->“Welcome to programmingguide.in”

Output->

Welcome

           to

           programmingguide.in

Here string is entered in the variable ‘str’ of type String by user and a string variable ‘s’ is declared.

Now we execute a function  split() which returns the array of strings computed by splitting the string around matches of the given regular expression.

At last we print the array of string  ’s’ using the for loop.

SOURCE CODE
SOURCE CODE

OUTPUT
OUTPUT

 

Author Description

humanz

Our mission is to bring the affordable Technology education to people everywhere, in order to help them achieve their dreams and change the world. - Follow @program_guide

No comments yet.

Join the Conversation