samedi 4 mai 2019

write a program that read a large text and count number of characters in this text , number of words and number of sentence

program that read a large text and count number of characters in this text , number of words and number of sentence

i tried to solve it by c++ but i can't because if i want to solve it i will need to the iterator (hasNext() and next()) that already exists in java but i can't find it in c++ Or even find a replacement for it

import java.io.*; 

public class Test 
{ 
   public static void main(String[] args) throws IOException 
   { 
       File file = new File("C:\\Users\\Mayank\\Desktop\\1.txt"); 
       FileInputStream fileStream = new FileInputStream(file); 
       InputStreamReader input = new InputStreamReader(fileStream); 
       BufferedReader reader = new BufferedReader(input); 

       String line; 

       // Initializing counters 
       int countWord = 0; 
       int sentenceCount = 0; 
       int characterCount = 0; 
       int paragraphCount = 1; 
       int whitespaceCount = 0; 

       // Reading line by line from the  
       // file until a null is returned 
       while((line = reader.readLine()) != null) 
       { 
           if(line.equals("")) 
           { 
               paragraphCount++; 
           } 
           if(!(line.equals(""))) 
           { 

               characterCount += line.length(); 

               // \\s+ is the space delimiter in java 
               String[] wordList = line.split("\\s+"); 

               countWord += wordList.length; 
               whitespaceCount += countWord -1; 

               // [!?.:]+ is the sentence delimiter in java 
               String[] sentenceList = line.split("[!?.:]+"); 

               sentenceCount += sentenceList.length; 
           } 
       } 

       System.out.println("Total word count = " + countWord); 
       System.out.println("Total number of sentences = " + sentenceCount); 
       System.out.println("Total number of characters = " + characterCount); 
       System.out.println("Number of paragraphs = " + paragraphCount); 
       System.out.println("Total number of whitespaces = " + whitespaceCount); 
   } 
}`


Total word count = 5
Total number of sentences = 3
Total number of characters = 21
Number of paragraphs = 2
Total number of whitespaces = 7

Aucun commentaire:

Enregistrer un commentaire