Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Word Mixer

Updated 21 Oct 2026

A word mixer builds a new word by taking one character alternately from two given words: the first letter of the first word, the first letter of the second word, the second letter of the first word, and so on. This version uses a class Mixer with a default constructor; feed() reads the words, mix() builds the result and display() prints it.

Definition

Take the characters at position $0, 1, 2, \ldots$ of both words in turn, the first word first. When one word is shorter, the remaining characters of the longer word are added at the end.

  • HELLO and WORLD (equal length) give HWEOLRLLOD.
  • ABC and 12345 give A1B2C345: after C3 the first word has ended, so 4 and 5 are added.

Only charAt(), length() and string concatenation with + are used.

Algorithm

  1. main creates an object ob of Mixer. The default constructor sets word1, word2 and mix to empty strings.
  2. main calls ob.feed(sc):
    • it reads the two words with nextLine();
    • it returns false if either word is empty, or if a loop with charAt() finds a space in either word; otherwise it returns true.
  3. If feed() returned false, main prints INVALID INPUT and stops.
  4. Otherwise main calls ob.mix():
    • l1 and l2 hold the lengths of the two words, and max the larger of them;
    • mix starts as an empty string;
    • for i from 0 to max - 1: if i < l1 the character word1.charAt(i) is added to mix; then if i < l2 the character word2.charAt(i) is added.
  5. main calls ob.display(), which prints the two words and the mixed word.

Program

The class has a data member mix and a method mix() with the same name; Java allows this because a variable and a method are different things, though a student may prefer different names.

import java.util.Scanner;

/* Class Mixer builds a new word by taking one character alternately from two words.
   The extra characters of the longer word are added at the end.
   Only charAt(), length() and string concatenation are used (no StringBuilder). */
public class Mixer
{
    String word1;   // data member: the first word
    String word2;   // data member: the second word
    String mix;     // data member: the mixed word built from word1 and word2

    /* Default constructor: sets all the words to empty strings. */
    Mixer()
    {
        word1 = "";
        word2 = "";
        mix = "";
    }

    /* Reads the two words from the keyboard.
       sc : the Scanner used for input
       returns true if both words are non-empty and contain no space, otherwise false */
    boolean feed(Scanner sc)
    {
        System.out.print("Enter the first word: ");
        word1 = sc.nextLine();
        System.out.print("Enter the second word: ");
        word2 = sc.nextLine();
        if (word1.length() == 0 || word2.length() == 0)     // an empty word is not allowed
        {
            return false;
        }
        for (int i = 0; i < word1.length(); i++)            // check word1 for spaces
        {
            if (word1.charAt(i) == ' ')
            {
                return false;
            }
        }
        for (int i = 0; i < word2.length(); i++)            // check word2 for spaces
        {
            if (word2.charAt(i) == ' ')
            {
                return false;
            }
        }
        return true;
    }

    /* Builds mix by taking one character alternately from word1 and word2.
       When one word ends, the rest of the longer word is still added. returns nothing */
    void mix()
    {
        int l1 = word1.length();            // length of the first word
        int l2 = word2.length();            // length of the second word
        int max = l1;                       // the longer length decides how many rounds are needed
        if (l2 > max)
        {
            max = l2;
        }
        mix = "";
        for (int i = 0; i < max; i++)
        {
            if (i < l1)                     // word1 still has a character at position i
            {
                mix = mix + word1.charAt(i);
            }
            if (i < l2)                     // word2 still has a character at position i
            {
                mix = mix + word2.charAt(i);
            }
        }
    }

    /* Prints the two words and the mixed word. returns nothing */
    void display()
    {
        System.out.println("First word: " + word1);
        System.out.println("Second word: " + word2);
        System.out.println("Mixed word: " + mix);
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);    // reads the input
        Mixer ob = new Mixer();                 // object of the class
        if (ob.feed(sc))
        {
            ob.mix();
            ob.display();
        }
        else
        {
            System.out.println("INVALID INPUT");
        }
    }
}

Variable description table

VariableData TypePurpose
word1StringData member that stores the first word
word2StringData member that stores the second word
mixStringData member that stores the mixed word
scScannerReads the words typed by the user; created in main and passed to feed()
iintLoop counter: the position of the character being checked or added
l1intLength of word1
l2intLength of word2
maxintThe larger of l1 and l2: the number of rounds of the loop in mix()
obMixerObject of the class created in main
argsString[]Command-line arguments of main (not used)

Sample input and output

Sample 1

Enter the first word: HELLO
Enter the second word: WORLD
First word: HELLO
Second word: WORLD
Mixed word: HWEOLRLLOD

Sample 2

Enter the first word: ABC
Enter the second word: 12345
First word: ABC
Second word: 12345
Mixed word: A1B2C345

Sample 3

Enter the first word: A
Enter the second word: XYZ
First word: A
Second word: XYZ
Mixed word: AXYZ

Sample 4

Enter the first word: SAME
Enter the second word: SAME
First word: SAME
Second word: SAME
Mixed word: SSAAMMEE

Sample 5

Enter the first word: HELLO
Enter the second word: WOR LD
INVALID INPUT

Sample 6

Enter the first word: HELLO
Enter the second word: 
INVALID INPUT

One value per line

Dry run

Take word1 = ABC and word2 = 12345. Here l1 = 3, l2 = 5 and max = 5. The loop in mix() runs as follows:

ii < l1 ?i < l2 ?Characters addedmix after the step
0yesyesA, 1A1
1yesyesB, 2A1B2
2yesyesC, 3A1B2C3
3noyes4A1B2C34
4noyes5A1B2C345

display() then prints Mixed word: A1B2C345, as in the second sample run.

Variations

  1. Reverse mix "Mix two words by taking the characters alternately from the end of each word."
  2. Two at a time "Mix two words by taking two characters at a time from each word."
  3. Capital letters "Mix two words and print the result in capital letters."