SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Special Fashion Sentence Program in Java and Python

03 February 2014

Special Fashion sentence program with algorithm, explanation, Java solution and simple Python solution for ISC students.

Question:

A sentence in the Special Fashion can be printed by taking two integers, not beyond the total number of words in the sentence and not less than 1. These integers tell the word numbers of the sentence. Replace only those words present at the given integer places by the next character in a circular fashion according to the English alphabet. If both integers are the same, then replace only one word.

Input Sentence: He has good Books. Input Integers: 2, 4 Output Sentence: He ibt good Cpplt. In this example, word number 2 and word number 4 have been replaced by the next characters in 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 the console and two positive integers. The characters of the sentence may be capital, small or mixed. The program should output the same sentence after replacing the words present at those given integer places by the next character in circular fashion according to the English alphabet.

In the first example, word number 2, has, is replaced by next characters and becomes ibt. Similarly, word number 4, Books, is replaced by next characters and becomes Cpplt.

Algorithm:

Step 1: Start.

Step 2: Accept a sentence from the user.

Step 3: Split the sentence into words using blank space and full stop as separators.

Step 4: Count the total number of words stored in the array.

Step 5: Accept two word numbers x and y.

Step 6: If either word number is less than 1 or greater than the total number of words, display an out-of-range message and go to Step 15.

Step 7: If x and y are different, send the word at position y to the character-replacement method.

Step 8: Send the word at position x to the character-replacement method.

Step 9: In the replacement method, read each character of the selected word.

Step 10: If the character is z, replace it by a; if it is Z, replace it by A.

Step 11: For any other alphabet, replace it by the next character.

Step 12: Return the changed word and store it back in the word array.

Step 13: Join all words from the array to form the final sentence.

Step 14: Display the final sentence with a full stop.

Step 15: Stop.

Explanation:

The program works with word positions rather than searching for particular words. After accepting the sentence, it separates the sentence into individual words and stores them in an array. The first word is considered position 1, the second word is position 2, and so on. Since Java arrays begin from index 0, word number x is stored at index x - 1. This index adjustment is important because the user enters natural word numbers, while the program uses array indexes internally.

Before changing any word, the program validates both input positions. If either number is less than 1 or greater than the number of words in the sentence, it cannot refer to an existing word. In that case, the program prints an out-of-range message and does not attempt replacement. This prevents invalid array access. If both numbers are valid, the selected words are sent to a separate method named repChar(), which performs the actual character shifting.

The replacement method reads one character at a time from the selected word. For ordinary characters from A to Y or a to y, the next character is obtained by increasing the character value by 1. Thus h becomes i, a becomes b, and B becomes C. The circular part is needed for z and Z. If z were simply increased, it would not become a. Therefore the method specially changes z to a and Z to A.

If the two word numbers are different, both words must be changed. If both numbers are the same, the word should be changed only once. The program handles this by changing the second selected word only when x != y, and then changing the first selected word. Finally, all words in the array are joined back in their original order. Only the selected words are modified; all other words remain unchanged. This method-based design keeps the main program focused on input, validation and sentence reconstruction, while the helper method handles alphabet shifting.

Java Program:

Java
/**
* The class Special_Fashion inputs a sentence and two word numbers.
* It replaces the selected words by shifting each character to the next alphabet.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class Special_Fashion
{
    String repChar(String word)
    {
        String result = "";

        for(int i = 0; i < word.length(); i++)
        {
            char ch = word.charAt(i);

            /*
            * The alphabet shift is circular.
            * z becomes a and Z becomes A.
            */
            if(ch == 'z')
                result = result + 'a';
            else if(ch == 'Z')
                result = result + 'A';
            else
                result = result + (char)(ch + 1);
        }

        return result;
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        Special_Fashion ob = new Special_Fashion();

        System.out.print("Enter any sentence: ");
        String sentence = sc.nextLine();

        /*
        * Words are separated using blank spaces and full stops.
        * This follows the original question examples.
        */
        String word[] = sentence.split("[. ]+");
        int count = word.length;

        System.out.print("Enter the first word number: ");
        int x = sc.nextInt();

        System.out.print("Enter the second word number: ");
        int y = sc.nextInt();

        if(x < 1 || y < 1 || x > count || y > count)
        {
            System.out.println("Sorry! The word numbers inputted are out of range");
        }
        else
        {
            /*
            * If both word numbers are different, change the second selected word.
            * If they are same, it should not be changed twice.
            */
            if(x != y)
            {
                word[y - 1] = ob.repChar(word[y - 1]);
            }

            word[x - 1] = ob.repChar(word[x - 1]);

            String answer = "";

            // Join all words to form the final sentence.
            for(int i = 0; i < count; i++)
            {
                answer = answer + word[i] + " ";
            }

            System.out.println("Output = " + answer.trim() + ".");
        }
    }
}

Equivalent Python Program:

Python
def replace_characters(word):
    result = ""

    for ch in word:
        # Apply circular alphabet shifting for z and Z.
        if ch == "z":
            result = result + "a"
        elif ch == "Z":
            result = result + "A"
        else:
            result = result + chr(ord(ch) + 1)

    return result


sentence = input("Enter any sentence: ")

# Remove full stops while splitting the sentence into words.
sentence = sentence.replace(".", " ")
words = sentence.split()
count = len(words)

x = int(input("Enter the first word number: "))
y = int(input("Enter the second word number: "))

if x < 1 or y < 1 or x > count or y > count:
    print("Sorry! The word numbers inputted are out of range")
else:
    # Change the second selected word only if both positions are different.
    if x != y:
        words[y - 1] = replace_characters(words[y - 1])

    words[x - 1] = replace_characters(words[x - 1])

    answer = ""

    # Join all words back into a sentence.
    for word in words:
        answer = answer + word + " "

    print("Output =", answer.strip() + ".")

Output:

Example 1: Enter any sentence: I love Java for School. Enter the first word number: 2 Enter the second word number: 5 Output = I mpwf Java for Tdippm. Example 2: Enter any sentence: I love Java for School Enter the first word number: 4 Enter the second word number: 4 Output = I love Java gps School. Example 3: Enter any sentence: I love Java for School Enter the first word number: 2 Enter the second word number: 6 Sorry! The word numbers inputted are out of range

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →