Future Date | ISC Computer Science 2027 Practical

Write a program that accepts:
a. A sentence terminated by either ‘.’, ‘?’ or ‘!’.
b. A date in the format DD/MM/YYYY.
Assume that words in the sentence are separated by a single blank space.

The program should perform the following tasks:
a. Validate the date and sentence entered. Display an appropriate error message if invalid.
b. Count and display the total number of vowels (V) present in the sentence.
c. Add V days to the given date and display the resulting future date.
d. Sort the words in ascending order of word length. For words having equal lengths, retain their original order of appearance.
e. Display the sorted words.

Example 1
INPUT:
Sentence: The sun is bright today.
Date: 20/01/2024
OUTPUT:
NUMBER OF VOWELS = 6
FUTURE DATE (AFTER 6 DAYS): 26/01/2024
SORTED WORDS BY LENGTH:
is The sun today bright

Example 2
INPUT:
Sentence: AI is amazing and useful?
Date: 28/02/2024
OUTPUT:
NUMBER OF VOWELS = 10
FUTURE DATE (AFTER 10 DAYS): 09/03/2024
SORTED WORDS BY LENGTH:
AI is and useful amazing

Example 3
INPUT:
Sentence: Good Morning everyone.
Date: 31/04/2024
OUTPUT:
INVALID DATE

import java.util.Scanner;
import java.util.StringTokenizer;
class FutureDate{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.print("Sentence: ");
        String s = in.nextLine();
        char last = s.charAt(s.length() - 1);
        if(".?!".indexOf(last) == -1){
            System.out.println("INVALID SENTENCE");
            return;
        }
        System.out.print("Date: ");
        String d = in.nextLine();
        int dd = Integer.parseInt(d.substring(0, d.indexOf('/')));
        int mm = Integer.parseInt(d.substring(d.indexOf('/') + 1, d.lastIndexOf('/')));
        int yyyy = Integer.parseInt(d.substring(d.lastIndexOf('/') + 1));
        int dn[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if(yyyy % 400 == 0 || (yyyy % 4 == 0 && yyyy % 100 != 0))
            dn[2] = 29;
        if(mm < 1 || mm > 12){
            System.out.println("INVALID DATE");
            return;
        }
        else if(dn[mm] < dd){
            System.out.println("INVALID DATE");
            return;
        }
        int v = 0;
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if("AEIOUaeiou".indexOf(ch) >= 0)
                v++;
        }
        System.out.println("NUMBER OF VOWELS = " + v);
        int f = v;
        while(v > 0){
            v--;
            if(dd == dn[mm]){
                mm++;
                if(mm > 12){
                    mm = 1;
                    yyyy++;
                    if(yyyy % 400 == 0 || (yyyy % 4 == 0 && yyyy % 100 != 0))
                        dn[2] = 29;
                    else
                        dn[2] = 28;
                }
                dd = 1;
            }
            else
                dd++;
        }
        System.out.println("FUTURE DATE (AFTER " + f + " DAYS): " + dd + "/" + mm + "/" + yyyy);
        StringTokenizer st = new StringTokenizer(s, " .?!,;:");
        int count = st.countTokens();
        String arr[] = new String[count];
        for(int i = 0; i < count; i++)
            arr[i] = st.nextToken();
        for(int i = 0; i < count; i++){
            for(int j = 0; j < count - 1 - i; j++){
                if(arr[j].length() > arr[j + 1].length()){
                    String temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println("SORTED WORDS BY LENGTH:");
        for(int i = 0; i < count; i++)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *