Sunday , 23 July 2017
Home » Class 12 » Program to replace words of a sentence in Special Fashion

Program to replace words of a sentence in Special Fashion

Question:

A sentence in the Special Fashion can be printed by taking two integers (not beyond total number of words in the sentence or less than 1). These integers tell the word number of the sentence. Replace only those words present at those given integer places by the next character in a circular fashion according to the English Alphabets. If both the integers are same then replace only one word. let us consider the following examples:

Input Sentence: He has good Books.
Input Integers: 2, 4
Output Sentence: He ibt good Cpplt.
(i.e. word number 2 and 4 have been replaced by the next characters in a circular fashion)

Input Sentence: Time and tide waits for none.
Input Integers: 3, 3
Output Sentence: Time and ujef waits for none.

Write a case sensitive program that reads a sentence from console (the characters of the sentence may be capital or small or mixed) and two positive integers and output the same sentence after replacing those words present at those given integer places by the next character in a circular fashion according to the English Alphabets.

In the first example given above, word number 2, i.e. “has” is replaced by next characters and hence it becomes “ibt”. Similarly, word number 4, i.e. “Books” is replaced by next characters and hence it becomes “Cpplt”.

Solution:

/**
* The class Special_Fashion inputs a sentence and two integers representing word numbers
* It then replaces the characters of those words by the next character in circular fashion
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Special_Fashion
{
    String repChar(String s) //function doing the work of replacing characters
    {
        int l = s.length(), a = 0;
        char ch;
        String res="";
        for(int i=0; i<l; i++)
        {
            ch = s.charAt(i); //extracting characters one by one
            a = ch + 1; //storing ASCII values after adding 1 to the current character
            if(ch=='z' || ch=='Z')
            {
                a = a - 26;
            }
            res = res + (char)a; //finally adding the changed character to the new String
        }
        return res;
    }
                
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
        Special_Fashion ob = new Special_Fashion();
        System.out.print("nEnter any sentence : "); //Inputting the sentence
        String s = br.readLine();
        String ans=""; //String variable to store the final result
        
        String word[]=s.split("[. ]+"); //saving the words of the sentence in an array using split()
        int c = word.length;
        
        System.out.print("Enter the 1st word number : ");
        int x = Integer.parseInt(br.readLine());
        System.out.print("Enter the 2nd word number : ");
        int y = Integer.parseInt(br.readLine());
        
        if(x<1 || y<1 || x>c || y>c) //checking whether integers inputted are acceptable or not
        {
            System.out.println("Sorry! The word numbers inputted are out of range");
        }
        else
        {
                if(x != y)
                {
                    word[y-1]=ob.repChar(word[y-1]); //sending the words to the repChar() function
                }
                word[x-1]=ob.repChar(word[x-1]);
                
                for(int i=0; i<c; i++)
                {
                    ans = ans + word[i] + " ";
                }
                System.out.print("Output = "+ans.trim()+".");
        }
    }
}

Output:

1) Enter any sentence : I love Java for School.
Enter the 1st word number : 2
Enter the 2nd word number : 5
Output = I mpwf Java for Tdippm.

2) Enter any sentence : I love Java for School
Enter the 1st word number : 4
Enter the 2nd word number : 4
Output = I love Java gps School.

3) Enter any sentence : I love Java for School
Enter the 1st word number : 2
Enter the 2nd word number : 6
Sorry! The word numbers inputted are out of range

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

3 comments

  1. could not understand the split part ……

    • Hello Rajeshwar,

      Hello Ritu,

      split() function splits a string into tokens (words) and returns the result in the form of a String array.
      Example: If String s = “Java For School”, then String w[]=s.split(” “); will save the words Java, For and School in the array w[]

      Just note: If there are 2 spaces in between any words or a fullstop followed by a space as in “Java is Fun. Said none”, the above code needs to be modified otherwise it will store null strings also in the array.
      Just use this syntax: String word[]=s.split(“[. ]+”);
      where ‘s’ is the sentence. ‘+’ denotes any possible combinations of the punctuators mentioned in the []. Similarly you can use other punctuation marks by putting them inside like this “[?!,;. ]+”

      Regards,
      guideforschool

Leave a Reply to Rajeshwar SIngha Cancel reply

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