Question Index

Write a program to calculate the number of lines and words in a given paragraph.

Input (String, For display purpose new line is shown as $) Output (String)
Programmers experienced with C and C++ can easily learn Java and rewrite the code.$Java and C/C++ will continue to coexist,$since Java solves certain problems and C/C++ solves certain other problems Lines = 3, Words = 32
Hi, this is Merit Campus, I am not the king of java$I am also not the emperor of java, I don't like to rule over people, I like to win their love Lines = 2, Words = 33
Sun rises in the east Lines = 1, Words = 5

class CountNumberOfLinesAndWordsInAParagraph

{    public static void main(String s[])
        String paragraph = "Java's primary motivation was platform independence and not internet.\r\nPlatform independence means the ability to write a program on one platform and run\r\nit on any other platform without worrying about the platform specific features. If it works on one platform,\r\nit should work on all other platforms.";
        System.out.println("The Paragraph details are : " + getParagraphDetails(paragraph));


public static ParagraphDetails getParagraphDetails(String paragraph) {
//Write code here to get the number of lines and number of words and return them.
class ParagraphDetails {

int lines;
int words;

public ParagraphDetails(int lines, int words) {
this.lines = lines;
this.words = words;

public String toString() {
return "Lines = " + lines + ", " + "Words = " + words;



Topic: Java String Arrays - String Arrays In Java

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App