[Question 9] ISC 2013 (Theory) Paper Solved

Question:

Design a class Exchange to accept a sentence and interchange the first alphabet with the last alphabet for each word in the sentence, with single letter word remaining unchanged. The words in the input sentence are separated by a single blank space and terminated by a full stop.

Example: Input: It is a warm day.
Output: tI si a marw yad

Some of the data members and member functions are given below:

Class name : Exchange

Data members/instance variables:
sent : stores the sentence
rev : to store the new sentence
size : stores the length of the sentence

Member functions:
Exchange() : default constructor
void readsentence() : to accept the sentence
void exfirstlast() : extract each word and interchange the first and the last alphabet of the word and form a new sentence ‘rev’ using the changed words
void display() : display the original sentence along with the new changed sentence.

Specify the class Exchange giving details of the constructor(), void readsentence(), void exfirstlast() and void display(). Define the main() function to create an object and call the function accordingly to enable the task.

Programming Code:

/**
* The class Exchange inputs a sentence and interchanges the first alphabet 
* with the last alphabet for each word in the sentence
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 2013 Question 9
*/

import java.io.*;
class Exchange
{
static BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
String sent, rev;
int size;

Exchange() //default constructor
   {
    sent="";
    rev="";
    size=0;
   }

void readsentence()throws IOException
   {
    System.out.print("Enter a sentence : ");
    sent=br.readLine();
    size=sent.length();
    if(sent.charAt(size-1)!='.') // checking whether the sentence ends in a fullstop or not.
    { //If not, then we are adding a fullstop and increasing the length by 1
    sent=sent+".";
    size=size+1;
    }
   }

void exfirstlast()
   {
    String s1=""; //temporary String variable for holding each word as it is extracted
    char ch;
    for(int i=0;i<size;i++)
     {
      ch=sent.charAt(i);
      if(ch!=' ' && ch!='.')
        {
         s1=s1+ch;
        }
      else //else will execute when 's1' will have a complete word in it
      {
       int l=s1.length(); //finding length of the extracted word
/*The Main Exchange Process Starts Here*/
       for(int j=0;j<l;j++)
        {
         if(j==0) //exchanging the first alphabet with the last
            ch=s1.charAt(l-1);
         else if(j==(l-1)) //exchanging the last alphabet with the first
            ch=s1.charAt(0);
         else
            ch=s1.charAt(j);
         rev=rev+ch; //adding the characters to the new String
        }
       rev=rev+" "; //after completion of a word, a space is added to the new String
       s1=""; //the temporary String variable is emptied
       }
     }
   }

void display()
   {
    System.out.println("The Original Sentence is : "+sent);
    System.out.println("The Changed Sentence is : "+rev);
   }

public static void main(String args[])throws IOException
   {
    Exchange ob=new Exchange();
    ob.readsentence();
    ob.exfirstlast();
    ob.display();
   }
} 

Output:

Enter a sentence : Today was a warm day.
The Original Sentence is : Today was a warm day.
The Changed Sentence is : yodaT saw a marw yad

Enter a sentence : This is a cat
The Original Sentence is : This is a cat.
The Changed Sentence is : shiT si a tac

A teacher whose aim is to make Java a fun to learn experience for the school students by striving to build clear fundamentals of every concepts and helping students to develop their ability to reason and logically analyze any given programs.

Your Turn To Talk

Your email address will not be published.