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.
HELLOandWORLD(equal length) giveHWEOLRLLOD.ABCand12345giveA1B2C345: afterC3the first word has ended, so4and5are added.
Only charAt(), length() and string concatenation with + are used.
Algorithm
maincreates an objectobofMixer. The default constructor setsword1,word2andmixto empty strings.maincallsob.feed(sc):- it reads the two words with
nextLine(); - it returns
falseif either word is empty, or if a loop withcharAt()finds a space in either word; otherwise it returnstrue.
- it reads the two words with
- If
feed()returnedfalse,mainprintsINVALID INPUTand stops. - Otherwise
maincallsob.mix():l1andl2hold the lengths of the two words, andmaxthe larger of them;mixstarts as an empty string;- for
ifrom 0 tomax - 1: ifi < l1the characterword1.charAt(i)is added tomix; then ifi < l2the characterword2.charAt(i)is added.
maincallsob.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
| Variable | Data Type | Purpose |
|---|---|---|
word1 | String | Data member that stores the first word |
word2 | String | Data member that stores the second word |
mix | String | Data member that stores the mixed word |
sc | Scanner | Reads the words typed by the user; created in main and passed to feed() |
i | int | Loop counter: the position of the character being checked or added |
l1 | int | Length of word1 |
l2 | int | Length of word2 |
max | int | The larger of l1 and l2: the number of rounds of the loop in mix() |
ob | Mixer | Object of the class created in main |
args | String[] | 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: HWEOLRLLODSample 2
Enter the first word: ABC
Enter the second word: 12345
First word: ABC
Second word: 12345
Mixed word: A1B2C345Sample 3
Enter the first word: A
Enter the second word: XYZ
First word: A
Second word: XYZ
Mixed word: AXYZSample 4
Enter the first word: SAME
Enter the second word: SAME
First word: SAME
Second word: SAME
Mixed word: SSAAMMEESample 5
Enter the first word: HELLO
Enter the second word: WOR LD
INVALID INPUTSample 6
Enter the first word: HELLO
Enter the second word:
INVALID INPUTDry run
Take word1 = ABC and word2 = 12345. Here l1 = 3, l2 = 5 and max = 5. The loop in mix() runs as follows:
| i | i < l1 ? | i < l2 ? | Characters added | mix after the step |
|---|---|---|---|---|
| 0 | yes | yes | A, 1 | A1 |
| 1 | yes | yes | B, 2 | A1B2 |
| 2 | yes | yes | C, 3 | A1B2C3 |
| 3 | no | yes | 4 | A1B2C34 |
| 4 | no | yes | 5 | A1B2C345 |
display() then prints Mixed word: A1B2C345, as in the second sample run.
Variations
- Reverse mix "Mix two words by taking the characters alternately from the end of each word."
- Two at a time "Mix two words by taking two characters at a time from each word."
- Capital letters "Mix two words and print the result in capital letters."