Java Practical Programs
Aim
Given an input string, you are expected to extract either all vowels, or all non-vowels from the string and return the result as all lowercase or uppercase, based on the options specified.
input1 represents the input string.
input2 represents the extraction option. 0 for extraction of all non-vowels. 1 for extraction of all vowels.
input3 represents the output case option. 0 for all lowercase letters. 1 for all UPPERCASE letters.
Implementation
Java Program for extraction of all vowels and non-vowels from a string.
import java.util.*;
public class Vovels {
public static void main(String[] args) {
String input1, vowelsString = "", nonVowelsString = "";
input1 = "Any string";
int input2 = 0;
int input3 = 1;
input1 = input1.toLowerCase();
for(int i = 0; i < input1.length(); i++) {
if(input1.charAt(i) == 'a' || input1.charAt(i) == 'e' || input1.charAt(i) == 'i' || input1.charAt(i) == 'o' || input1.charAt(i) == 'u') {
vowelsString += input1.charAt(i);
} else {
nonVowelsString += input1.charAt(i);
}
}
if(input2 == 0)
if(input3 == 0)
System.out.println(nonVowelsString);
else {
System.out.println(nonVowelsString.toUpperCase());
}
else
if(input3 == 0)
System.out.println(vowelsString);
else
System.out.println(vowelsString.toUpperCase());
}
}
Result
Place your ad here