


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



//This program written by Anh Tuan Ho import java.util.*; public class TextAnalysis { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Get text input from user System.out.print("Enter a paragraph or text: "); String text = scanner.nextLine(); // Check if input is empty if (text.trim().isEmpty()) { System.out.println("Error: Text cannot be empty!"); scanner.close(); return; } // Character count int charCount = text.length(); System.out.println("Total characters: " + charCount); // Word count String[] words = text.trim().split("\s+"); int wordCount = words.length; System.out.println("Total words: " + wordCount);
// Most common character, includding spaces String lowerText = text.toLowerCase(); int[] charFreq = new int[ 256 ]; // Count frequency of all characters for (char c : lowerText.toCharArray()) { charFreq[c]++; } char mostCommon = ' '; int maxCount = 0 ; for (int i = 0 ; i < charFreq.length; i++) { if (charFreq[i] > maxCount) { maxCount = charFreq[i]; mostCommon = (char) i; } } System.out.println("Most common character: '" + mostCommon + "' (" + maxCount + " times)"); // Character frequency search System.out.print("Enter a character to find its frequency: "); String charInput = scanner.nextLine(); if (!charInput.isEmpty()) { char searchChar = charInput.toLowerCase().charAt( 0 );