Java Program to count number of characters, alphabets, digits, words and vowels in a paragraph
// Program to count number of characters, alphabets,
// digits, words and vowels in a paragraph
class CountInSentence {
static int count(String paragraph) {
return paragraph.length();
}
static int countAlphabets(String paragraph) {
int count = 0;
for (int i = 0; i < paragraph.length(); i++) {
char ch = paragraph.charAt(i);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
count++;
}
return count;
}
static int countDigits(String paragraph) {
int count = 0;
for (int i = 0; i < paragraph.length(); i++) {
char ch = paragraph.charAt(i);
if (ch >= '0' && ch <= '9')
count++;
}
return count;
}
static int countWords(String paragraph) {
return paragraph.split(" ").length;
}
static int countVowels(String paragraph) {
int count = 0;
for (int i = 0; i < paragraph.length(); i++) {
char ch = paragraph.charAt(i);
if (ch == 'a' || ch == 'i' || ch == 'e' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
count++;
}
return count;
}
public static void main(String[] args) {
String paragraph = "Java is a general-purpose computer programming language " +
"that is concurrent, class-based, object-oriented, and specifically designed " +
"to have as few implementation dependencies as possible. It is intended to let " +
"application developers \"write once, run anywhere\" (WORA), meaning that " +
"compiled Java code can run on all platforms that support Java without the " +
"need for <recompi></recompi>lation. Java applications are typically compiled " +
"to bytecode that can run on any Java virtual machine (JVM) regardless of "+
"computer architecture. As of 2016, Java is one of the most popular programming " +
"languages in use, particularly for client-server web applications, with a reported " +
"9 million developers. Java was originally developed by James Gosling at " +
"Sun Microsystems (which has since been acquired by Oracle Corporation) and " +
"released in 1995 as a core component of Sun Microsystems' Java platform. " +
"The language derives much of its syntax from C and C++, but it has fewer " +
"low-level facilities than either of them.";
System.out.println("Number of characters in the paragraph = " + count(paragraph));
System.out.println("Number of alphabets in the paragraph = " + countAlphabets(paragraph));
System.out.println("Number of digits in the paragraph = " + countDigits(paragraph));
System.out.println("Number of words in the paragraph = " + countWords(paragraph));
System.out.println("Number of vowels in the paragraph = " + countVowels(paragraph));
}
}
// digits, words and vowels in a paragraph
class CountInSentence {
static int count(String paragraph) {
return paragraph.length();
}
static int countAlphabets(String paragraph) {
int count = 0;
for (int i = 0; i < paragraph.length(); i++) {
char ch = paragraph.charAt(i);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
count++;
}
return count;
}
static int countDigits(String paragraph) {
int count = 0;
for (int i = 0; i < paragraph.length(); i++) {
char ch = paragraph.charAt(i);
if (ch >= '0' && ch <= '9')
count++;
}
return count;
}
static int countWords(String paragraph) {
return paragraph.split(" ").length;
}
static int countVowels(String paragraph) {
int count = 0;
for (int i = 0; i < paragraph.length(); i++) {
char ch = paragraph.charAt(i);
if (ch == 'a' || ch == 'i' || ch == 'e' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
count++;
}
return count;
}
public static void main(String[] args) {
String paragraph = "Java is a general-purpose computer programming language " +
"that is concurrent, class-based, object-oriented, and specifically designed " +
"to have as few implementation dependencies as possible. It is intended to let " +
"application developers \"write once, run anywhere\" (WORA), meaning that " +
"compiled Java code can run on all platforms that support Java without the " +
"need for <recompi></recompi>lation. Java applications are typically compiled " +
"to bytecode that can run on any Java virtual machine (JVM) regardless of "+
"computer architecture. As of 2016, Java is one of the most popular programming " +
"languages in use, particularly for client-server web applications, with a reported " +
"9 million developers. Java was originally developed by James Gosling at " +
"Sun Microsystems (which has since been acquired by Oracle Corporation) and " +
"released in 1995 as a core component of Sun Microsystems' Java platform. " +
"The language derives much of its syntax from C and C++, but it has fewer " +
"low-level facilities than either of them.";
System.out.println("Number of characters in the paragraph = " + count(paragraph));
System.out.println("Number of alphabets in the paragraph = " + countAlphabets(paragraph));
System.out.println("Number of digits in the paragraph = " + countDigits(paragraph));
System.out.println("Number of words in the paragraph = " + countWords(paragraph));
System.out.println("Number of vowels in the paragraph = " + countVowels(paragraph));
}
}
Comments
Post a Comment
Post Your Valuable Comments