Friday, March 11, 2022

Count Vowels and Consonants Using Scala

 Machine Problem

Write a program to ask the users to give a string or a sentence then the program will count the number of vowels, and consonants and display the results on the screen.

 I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.


My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

Please subscribe to my channel  https://www.youtube.com/channel/UCOs-lpOoIeJoh6gpJthPoGg

Thank you very much for your support.





Program Listing

Problem No. 7

Write a program to ask the users to give a string or a sentence
then the program will count the number of vowels, and consonants
and display the results on the screen.

Solution

/* vowels_consonants.scala
   Prof. Jake Rodriguez Pomperada, MAED-IT, MIT
   www.jakerpomperada.blogspot.com and www.jakerpomperada.com
   jakerpomperada@gmail.com
   March 9, 2022  3:12 PM    Wednesday
   Bacolod City, Negros Occidental
 */

import java.util.Scanner;

object vowels_consonants {


   def main(args: Array[String]) : Unit = {

    var input = new Scanner(System.in);

   var vowels = 0; var consonants = 0;
  
   var a = 0;

    print("\n");
    print("\tCount Vowels and Consonants Using Scala");
    print("\n\n");
    print("\tGive a String or a Sentence :  ");
    var line = input.nextLine();

    line = line.toLowerCase();
    
    while (a < line.length) 
    {
      var ch = line.charAt(a);

      // check if character is any of a, e, i, o, u
      if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
        vowels=vowels+1;
      }

      // check if character is in between a to z
      else if ((ch >= 'a' && ch <= 'z')) {
        consonants=consonants+1;
      }
      
    a=a+1;
    }
    print("\n")
    print("\t===== DISPLAY RESULTS ====\n\n");
    printf("\tNumber of Vowels     : %d \n" ,vowels);
    printf("\tNumber of Consonants : %d " ,consonants);
    print("\n\n");
    print("\tEND OF PROGRAM");
    print("\n\n");
  }
  
 
  }

No comments:

Post a Comment