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 2014 solved

Monday, July 28, 2014

ISC Computer Science Practical 2014 solved



/*
Question 3.
Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only. Any
other character may be ignored. The words may be separated by more than one blank space and are
in UPPER CASE.
Perform the following tasks:
a) Accept a sentence and reduce all the extra blank space between 2 words to a single blank space.
b) Accept a word from the user which is a part of the sentence along with its position number
and delete the word and display the sentence.

Test your program for the following data and some random data.:

Example 1
INPUT: A   MORNING WALK IS A BLESSING FOR   THE    WHOLE DAY.
WORD TO BE DELETED : IS
WORD POSITION IN THE SENTENCE : 4
OUTPUT: A MORNING WALK A BLESSING FOR THE WHOLE DAY.

Example 2
INPUT: AS YOU SOW, SO SO YOU REAP.
WORD TO BE DELETED : SO
WORD POSITION IN THE SENTENCE : 4
OUTPUT: AS YOU SOW, SO YOU REAP.

Example 3
INPUT: STUDY WELL ##.
OUTPUT: INVALID INPUT.
/*

/* ISC COMPUTER SCIENCE PRACTICAL~2014 */
import java.util.*;
import java.io.*;
public class SpaceWord {
    public static void main(String[] args) throws IOException
        {
        String msg;
        String wordDelete;
        int pos;
        Scanner in = new Scanner(System.in);
        System.out.println("WORD POSITION IN THE SENTENCE:");
        pos = in.nextInt();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
        System.out.println(" Enter the sentence:");
        msg = br.readLine();
        br.close();
        OutputString os = new OutputString(msg);
        if(os.validInvalid() == false)
            System.out.println("INVALID INPUT");
        else    {
            os.extraSpaceDeletion();
            os.wordDeletion(pos);
            }
               
        }
    }
class OutputString {
    OutputString(String mg) {
        msg = mg;
        lengthMsg = msg.length();
        outputmsg = "";
        }
    public boolean validInvalid() {
        return (msg.charAt(lengthMsg-1) == '.'  ? true : msg.charAt(lengthMsg-1) == '!' ? true : msg.charAt(lengthMsg-1) == '?' ? true : false);
        }   
    public void extraSpaceDeletion() {
        for ( int i = 0; i < lengthMsg; i++) {
            if(msg.charAt(i) != ' ') {
                outputmsg += msg.charAt(i);
                }
            else {
                outputmsg += msg.charAt(i);
                while(msg.charAt(i) == ' ' && i < lengthMsg)
                    i++;
                i--;   
                }   
            }
        System.out.println();   
        }   
    public void wordDeletion(int pos) {
        int tmp = 0;
        for ( int i = 0; i < outputmsg.length(); i++) {
            if(outputmsg.charAt(i) == ' ')
                tmp++;
            if(tmp == pos-1) {
                while(outputmsg.charAt(i+1) != ' ' && i < outputmsg.length())
                    i++;
                }   
            else
            System.out.print(outputmsg.charAt(i));   
            }
        System.out.println();
        }   
    private String msg;
    private String outputmsg;
    private int lengthMsg;
    }       

No comments:

Post a Comment