Analyzed the String
Previous | Home | Next |
Following program for Analyzed the String by finding the vowel, spaces and consonants.
/* * Save as a StringCheck.java * Program for Analyze the String. */ package r4r.co.in; import java.io.*; public class StringCheck { public static void main(String[] args) throws IOException { //Initilized the variable = 0 int spaces = 0, // Count for spaces letters = 0, // Count for letters vowels = 0; // Count for vowels //Input the String from the Console. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 150); System.out.println("Write a Paragraph, not more than 150 word:"); String text = reader.readLine(); //Now, Analyze all the characters in the string for (int i = 0; i < text.length(); i++) { // Check for vowels in the String char ch = Character.toLowerCase(text.charAt(i)); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { ++vowels; } //Check for letters in the String if (Character.isLetter(ch)) { ++letters; } // Check for spaces in the String if (Character.isWhitespace(ch)) { ++spaces; } } System.out.println("\n Total Length/Letter of the String:" + letters); System.out.println(" The text contained vowels: " + vowels + "\n consonants: " + (letters - vowels) + "\n spaces:" + spaces); } }
Write a Paragraph, not more than 150 word:
Hey! Is this program is really working.....Total Length/Letter of the String:31
The text contained vowels: 10
consonants: 21
spaces:6
Previous | Home | Next |