Menu
Topics Index
...
`

Count the number of lines and number of words in a given paragraph


If you need explanation Read this topic

If you need Answer Take test on this topic

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) {
    }

}
class ParagraphDetails {

int lines;
int words;

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

@Override
public String toString() {
return "Lines = " + lines + ", " + "Words = " + words;
}
}
Topic: Java String Arrays - String Arrays In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Nice One Merit Campus ... It make me think from very basic ..  According to Requirement ...
when we see the getParagraphDetails() method.. it has return type of "ParagraphDetails".. So we need to create an object for it and has to Save Details of lines and words.. that too we need to invoke it with an "Argumented Constuctor" (Be Careful while Passing args .. as last word doesn't end with space and last line doesn't end with "\n" .. literally we need to pass 1,1) ..
Now Convert it into Char sequence
then we need to run the for loop and need to increment the lines field of the object when if condition satisfies the "CURRENT CHARACHTER  == "\n" " .. and need to increment the words of the Object when if Condition Satisfies the "Current char == space  or "\n" "..
now return the "ParagraphDetails Object"...

Posted by ?????????? ?????    2015-01-28 06:43:11


in any given paragraph,no. of words=no.of spaces in paragraph     (plus) 1..... hence no.  of words can be counted by counting spaves in that paragraph.. now to count lines we have to chek whether the character=\n... if yes den increment the line counter....... coming to program... firstly take space=1 and line=1......now here use a loop which will traverse thru every character of paragraph.... for i=0to length.string.. inside this loop chek if charAt(i)=" " ...den increment spce by one.... den chek whether charAt(i)=\n den increment line counter by one.... den exit loop and print space and line....

Posted by Asma Mujtaba Khan    2015-01-28 08:06:31


Now a days space complexity is not an big problem..so we can do it using by spliting the String..need to take 2 string arrays

* create string array..and store the paragraph by spliting it into lines..
String temp[]=input.split("\n");
int lines=temp.length+1;
* now split the paragraph by passing the " " as delimiter
temp[]=input.split(" ");

int words=temp.length+lines-1;
* create the instance for the ParagraphDetails class by passing the lines and words values..
last return the instance

Posted by Uday Kumar    2015-01-28 15:23:13


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Second Correct Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-29 06:38:27


© meritcampus 2019

All Rights Reserved.

Open In App