Write a Java program that displays the number of characters, lines and words in a text

Java program that displays the number of characters line word . Counting the number of characters is important because almost all the text boxes that rely on user input have certain limit on the number of characters that can be inserted. For example, the character limit on a Facebook post is 63, 206 characters. Whereas, for a tweet on Twitter the character limit is 140 characters and the character limit is 80 per post for Snapchat.

Determining character limits become crucial when the tweet and Facebook post updates are being done through api’s. Java program that displays the number of characters line word

                                           Week – 4
c)Aim: To Write a Java program that displays the number of characters, lines and words in a text.
 
Source Code:
import java.io.*;
class FILECH
{
           static int lines=0,words=0,chars=0;
           public static void wordCount(InputStreamReader isr)throws IOException
           {
                  int c=0;
                  while((c=isr.read())!=-1)
                  {
                  chars++;
                  if(c==’n’)                                                                                                         
                  lines++;
                  if((c==’n’)||(c==’ ‘)||(c==’t’))
                  ++words;
                  }
           }     
           public static void main(String rr[])throws IOException
           {
                  if(rr.length==0)
                  {
                                                                                                                                               System.out.println(“Enter the Text:”);
                  wordCount(new InputStreamReader(System.in));
                  }
                  else
                  {
                  FileReader fr=new FileReader(rr[0]);
                  wordCount(fr);
                  }
                  System.out.println(“Number of Words:”+words);
                  System.out.println(“Number of charecters:”+chars);                                              
                  System.out.println(“Number of lines: “+lines);
           }
}
 
Expected Output:
 
D:java>java FILECH
Enter the Text:
hai how r u ??
//////press Ctrl+z 
Number of Words:5
Number of charecters : 16
Number of lines: 1

sites: https://www.myprogrammingschool.com/

https://en.wikipedia.org/wiki/Characters_per_line

Leave a Comment

Your email address will not be published. Required fields are marked *