Header Ads

Define a class to declare a character array of size ten, accept the character into the array and perform the following: • Count the number of uppercase letters in the array and print. • Count the number of vowels in the array and print.



In order to count upper and lower case character we have to first find weather a given character is in upper case or in lower case.For this we would take concept of ASCII value of each character in Java.

In Java as we know for each character has corresponding ASCII value so we would compare each character that it lies in the range of upper case or in lower case.
We will traverse through the array in a for loop starting from index 0 till size-1.And check each character if it is a vowel or UpperCase and increment the count variables.


Code :

import java.util.Scanner;

public class count {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char up[] = new char[10];
char vow[] = new char[10];
char arr[] = new char[10];
System.out.println("Enter characters ");
String s = sc.nextLine();
s.getChars(0,10,arr,0);

int upper=0, vowel=0;
for (int i = 0; i < 10; i++) {
if (arr[i]>= 'A' && arr[i]<='Z') {
if (arr[i] == 'A' || arr[i] == 'E' || arr[i] == 'I' || arr[i] == 'O' || arr[i] == 'U') {
vow[vowel] = arr[i];
up[upper] = arr[i];
upper++;
vowel++;
}
else{
up[upper] = arr[i];
upper++;
}
}
else if (arr[i] == 'a' || arr[i] == 'e' || arr[i] == 'i' || arr[i] == 'o' || arr[i] == 'u') {
vow[vowel] = arr[i];
vowel++;
}
}

System.out.println("No. of UpperCase Letters : "+upper);
System.out.print("UpperCase Letters : ");
for (int j=0; j<upper; j++){
System.out.print(up[j]);
}
System.out.println();
System.out.println("No. of Vowels : "+vowel);
System.out.print("Vowels : ");
for (int j=0; j<vowel; j++){
System.out.print(vow[j]);
}
}
}

Output :

Enter characters 
AbhayKumar
No. of UpperCase Letters : 2
UpperCase Letters : AK
No. of Vowels : 4
Vowels : Aaua

Explanation :

After importing necessary libraries and declaring main function create a scanner object. Declare 3 arrays up, vow and arr for storing Uppercase letters, vowels and characters respectively. Take the size of all the arrays 10 as total no. of characters will be 10. For a instance all the characters can be Uppercase or vowel so as a coder we should be prepared for that. Now give a prompt to Enter characters and store them in 's' as a string. Here will be using nextLine() to parse the string, if we use next() it will read the string upto 'space'. 
We will use getChars() method to convert string to Char Array, this method takes 4 parameters i.e., source beginning index, end index, array, array index in which first value to be inserted.
Declare 2 variable 'upper' and 'vowel' of integer type with initial value 0. These are used to store the count of Uppercase characters and vowels. Use for loop to traverse through the array use if else if  statements to find uppercase letters and vowels. After that write the code to print the desired output.
use for loop to print 'up' and 'vow' array.

Post a Comment

0 Comments