Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2015 QUESTION 3

Tuesday, March 12, 2019

ISC COMPUTER SCIENCE PRACTICAL 2015 QUESTION 3

/* Write a program to accept a sentence which may be terminated by either ‘.’ Or ‘?’ only.
The words are to be separated by a single blank space. Print an error message if the input
does not terminate with ‘.’ Or ‘?’. You can assume that no word in the sentence exceeds
15 characters, so that you get a proper formatted output. Perform the following tasks:
i) Convert the first letter of each word to uppercase.
ii) Find the number of vowels and consonants in each word and display them
with proper heading along with the words.
Test your program with the following inputs:

Example 1
INPUT : Intelligence plus character is education.
OUTPUT:
Intelligence plus character is education.
Word Vowels Consonants
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
Example 2
INPUT : God is great.
OUTPUT:
God is great
Word Vowels Consonants
God 1 2
Is 1 1
Great 2 3   */



import java.io.*;
public class ISC2015Q3 {
       public static void main(String[] args) throws IOException{
             String Sentence;
             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
             System.out.println("Enter the sentence:");
             Sentence=br.readLine();
             while(Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '.' ) {
                    System.out.println("INVALID INPUT:,TRY AGAIN");
                    Sentence=br.readLine();
             }
             br.close();
             WordExtractor we = new WordExtractor(Sentence);
             we.WordCount();
             we.ExtractWord();
             we.Output();
             }
}
class WordExtractor {
       WordExtractor(String s){
             msg=s;
             Word_Count=0;
             Vowel_Count=0;
             }
       public void Output() {
             System.out.println("WORD           "+"Vowels     "+"Consonants");
             for(int i=0;i<Word_Count;i++) { // Inspecting each word
                    char[] ch = Word_Storage[i].toCharArray(); // using library function- converting string to char array
                    if (ch[0] >= 'a' && ch[0] <= 'z')  
                          ch[0] = (char)(ch[0] - 'a' + 'A'); //First letter of each word to upper case
                    for(int j=0; j<Word_Storage[i].length();j++) { // counting vowels in individual word
                          for(int l=0; l<9;l++)
                                 if(Word_Storage[i].charAt(j) == vowels[l])
                                       Vowel_Count++;
                                 }
                    Word_Storage[i] = new String(ch);
                    String temp=Word_Storage[i];
                    for(int t=0;t<15-Word_Storage[i].length();t++)  // just for proper formatted output
                    temp +=" ";
                    System.out.println(temp+"     "+Vowel_Count+"          "+(Word_Storage[i].length()-Vowel_Count));
                    Vowel_Count=0;
             }
       }
       public void ExtractWord() {
             int k = 0;
             for(int i=0;i<msg.length();i++) {
                    Word_Storage[k]="";
                    while(msg.charAt(i) != ' ' && msg.charAt(i) != '?' && msg.charAt(i) != '.') {
                          Word_Storage[k] +=msg.charAt(i);
                          i++;
                          }
                    System.out.println();
                    k++;
             }
       }

       public void WordCount() {
             for(int i=0;i<msg.length();i++) {
                    if(msg.charAt(i)==' ')
                          Word_Count++;
                          }
              Word_Count +=1;
              Word_Storage = new String[Word_Count];
       }
       private char[] vowels = {'a','e','i','o','u','A','E','I','O','u'};
       private String msg;
       private String[] Word_Storage;
       private int Word_Count;
       private int Vowel_Count;
}

No comments:

Post a Comment