Computer Applications
Define a class to accept a string. Check if it is a Special String or not.
A String is Special if the number of vowels equals to the number of consonants.
Example: MINE
Number of vowels = 2
Number of Consonants = 2
Java
Java String Handling
4 Likes
Answer
import java.util.Scanner;
public class SpecialString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine().toUpperCase();
int vowels = 0, consonants = 0;
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
if (ch == 'A'
|| ch == 'E'
|| ch == 'I'
|| ch == 'O'
|| ch == 'U')
vowels++;
else
consonants++;
}
}
System.out.println("Number of vowels = " + vowels);
System.out.println("Number of consonants = " + consonants);
if (vowels == consonants)
System.out.println("It is a Special String");
else
System.out.println("It is not a Special String");
}
}Output


Answered By
3 Likes
Related Questions
Define a class to accept values into a 3x3 integer array and print the product of each row elements.
Example:
3 1 2 4 2 1 5 1 2 Output:
Row 0 – 6
Row 1 – 8
Row 2 – 10Define a class to overload the method transform as follows:
int transform(int n) – to return the sum of the digits of the given number
Example: n = 458
output : 17void transform(String s) – to convert the given String to upper case and print
Example: if S = “Blue”
Output : BLUEvoid transform (char ch) – to print the character ch in 3 rows and 3 columns using nested loops.
Example: if ch = ‘@’
Output :@@@ @@@ @@@Define a class to accept a number and check if the sum of the first digit and the last digit is an even number or an odd number.
Example:
N = 2396 N = 9316 First digit: 2 9 Last digit: 6 6 Sum: 8 15 Output: Sum is even Sum is oddDefine a class to accept 10 integers in an array, search for the given value using the Linear Search technique and print appropriate messages.