KnowledgeBoat Logo

Computer Applications

Your friend is trying to write a program to find and display the frequency of vowels in a string. Due to confusion in the syntax of some of the statements, he could not complete the program and has left some places blank marked with ?1?, ?2?, ?3? and ?4? to be filled with expression/function. The incomplete program is shown below:

import java.util.*; 
class Vowels
{
  public static void main(String args[])
  {
    Scanner ...?1?... = new Scanner(System.in);
    String st;
    int i, b, v = 0;
    char chr;
    System.out.println("Enter a string");
    st = ............?2?............; // to input a string
    st = ............?3?...........; // to convert the string into uppercase
    b = st.length();
    for( i = 0 ; i < b ; i++)
    {
      chr = ............?4?............; // to extract a character
      if(chr == 'A' || chr == 'E' || chr == 'I' || chr == 'O' || chr == 'U')
        v = v + 1;
    }
    System.out.println("No. of Vowels = " + v);
  }
}

Based on the above discussion, answer the following questions:

(a) What expression/function will be filled in place of ?1?

(b) What expression/function will be filled in place of ?2?

(c) What expression/function will be filled in place of ?3?

(d) What expression/function will be filled in place of ?4?

Java String Handling

9 Likes

Answer

(a) in

(b) in.nextLine();

(c) st.toUpperCase();

(d) st.charAt(i);

The completed program is given below for reference:

import java.util.*; 
class Vowels
{
  public static void main(String args[])
  {
    Scanner in = new Scanner(System.in);
    String st;
    int i, b, v = 0;
    char chr;
    System.out.println("Enter a string");
    st = in.nextLine(); // to input a string
    st = st.toUpperCase(); // to convert the string into uppercase
    b = st.length();
    for( i = 0 ; i < b ; i++)
    {
      chr = st.charAt(i); // to extract a character
      if(chr == 'A' || chr == 'E' || chr == 'I' || chr == 'O' || chr == 'U')
        v = v + 1;
    }
    System.out.println("No. of Vowels = " + v);
  }
}

Answered By

5 Likes


Related Questions