//*********************************************************
// VowelCount.java
//
// This program counts the number of vowels in a string
//*********************************************************
import java.util.Scanner;
public class VowelCount {
public static void main(String[] args) {
/*
* declare variables to store the number of each type of vowel
*/
int countA = 0, countE = 0, countI = 0, countO = 0, countU = 0;
Scanner scan = new Scanner(System.in);
// Get the string from the user (prompt and input)
System.out.println("Enter a string of characters:");
String str = scan.nextLine();
// format for this part:
for (int i = 0; i < str.length(); i++) {
switch (str.charAt(i)) {
case 'a':
case 'A':
countA++;
break;
case 'e':
case 'E':
countE++;
break;
case 'i':
case 'I':
countI++;
break;
case 'o':
case 'O':
countO++;
break;
case 'u':
case 'U':
countU++;
break;
default:
break;
}
}
/*
* Remember: 1) condition should use method for getting string length 2)
* exp (in switch) should use method for returning a character
*/
// Output results
System.out.println("Number of each vowel in the string: a: " + countA
+ " e: " + countE + " i: " + countI + " o: " + countO
+ " u: " + countU);
}
}
OUTPUT:
Enter a string of characters:
Hello! I am Finn from the land of Ooo.
Number of each vowel in the string:
a: 2
e: 2
i: 2
o: 6
u: 0
Solution
//*********************************************************
// VowelCount.java
//
// This program counts the number of vowels in a string
//*********************************************************
import java.util.Scanner;
public class VowelCount {
public static void main(String[] args) {
/*
* declare variables to store the number of each type of vowel
*/
int countA = 0, countE = 0, countI = 0, countO = 0, countU = 0;
Scanner scan = new Scanner(System.in);
// Get the string from the user (prompt and input)
System.out.println("Enter a string of characters:");
String str = scan.nextLine();
// format for this part:
for (int i = 0; i < str.length(); i++) {
switch (str.charAt(i)) {
case 'a':
case 'A':
countA++;
break;
case 'e':
case 'E':
countE++;
break;
case 'i':
case 'I':
countI++;
break;
case 'o':
case 'O':
countO++;
break;
case 'u':
case 'U':
countU++;
break;
default:
break;
}
}
/*
* Remember: 1) condition should use method for getting string length 2)
* exp (in switch) should use method for returning a character
*/
// Output results
System.out.println("Number of each vowel in the string: a: " + countA
+ " e: " + countE + " i: " + countI + " o: " + countO
+ " u: " + countU);
}
}
OUTPUT:
Enter a string of characters:
Hello! I am Finn from the land of Ooo.
Number of each vowel in the string:
a: 2
e: 2
i: 2
o: 6
u: 0

Vo.pdf

  • 1.
    //********************************************************* // VowelCount.java // // Thisprogram counts the number of vowels in a string //********************************************************* import java.util.Scanner; public class VowelCount { public static void main(String[] args) { /* * declare variables to store the number of each type of vowel */ int countA = 0, countE = 0, countI = 0, countO = 0, countU = 0; Scanner scan = new Scanner(System.in); // Get the string from the user (prompt and input) System.out.println("Enter a string of characters:"); String str = scan.nextLine(); // format for this part: for (int i = 0; i < str.length(); i++) { switch (str.charAt(i)) { case 'a': case 'A': countA++; break; case 'e': case 'E': countE++; break; case 'i': case 'I': countI++; break; case 'o': case 'O': countO++; break;
  • 2.
    case 'u': case 'U': countU++; break; default: break; } } /* *Remember: 1) condition should use method for getting string length 2) * exp (in switch) should use method for returning a character */ // Output results System.out.println("Number of each vowel in the string: a: " + countA + " e: " + countE + " i: " + countI + " o: " + countO + " u: " + countU); } } OUTPUT: Enter a string of characters: Hello! I am Finn from the land of Ooo. Number of each vowel in the string: a: 2 e: 2 i: 2 o: 6 u: 0 Solution //********************************************************* // VowelCount.java // // This program counts the number of vowels in a string //********************************************************* import java.util.Scanner;
  • 3.
    public class VowelCount{ public static void main(String[] args) { /* * declare variables to store the number of each type of vowel */ int countA = 0, countE = 0, countI = 0, countO = 0, countU = 0; Scanner scan = new Scanner(System.in); // Get the string from the user (prompt and input) System.out.println("Enter a string of characters:"); String str = scan.nextLine(); // format for this part: for (int i = 0; i < str.length(); i++) { switch (str.charAt(i)) { case 'a': case 'A': countA++; break; case 'e': case 'E': countE++; break; case 'i': case 'I': countI++; break; case 'o': case 'O': countO++; break; case 'u': case 'U': countU++; break; default: break; }
  • 4.
    } /* * Remember: 1)condition should use method for getting string length 2) * exp (in switch) should use method for returning a character */ // Output results System.out.println("Number of each vowel in the string: a: " + countA + " e: " + countE + " i: " + countI + " o: " + countO + " u: " + countU); } } OUTPUT: Enter a string of characters: Hello! I am Finn from the land of Ooo. Number of each vowel in the string: a: 2 e: 2 i: 2 o: 6 u: 0