Question
Below is a script that completes the following: Perform 4-letter WORD UNSCRAMBLING i.e. List all possible combinations of 4-letters in a word. Example: The word
Below is a script that completes the following: Perform 4-letter WORD UNSCRAMBLING i.e. List all possible combinations of 4-letters in a word. Example: The word 'TEST' can be unscrambled as TEST, TETS, TSET, TSTE, TTSE, TTES, etc.
#include "stdafx.h"
#include
#include
using namespace std;
int main()
{
cout << "Enter a word to see how many ways you can arrange its letters." << endl;
string word;
cin >> word; //taking in the word
int length = word.length(); //calculating the length of the input word and storing it to variable name "length"
int arrangements = 1; //initilize a variable "arrangments" or else it will contain junk value
for (int i = 1; i <= word.length(); i++) { //iterating over the elements of the word and finding the maximum number of possible cominations
arrangements *= i; //initially i=1, arrangments *= is equal to arrangments*i
/*
first iteration i=1 --> arrangments = 1
second iteration i=2 --> arrangments = 1*2=2
third iteration i=3 --> arrangments = 2*3=6
fourth iteration i=4 --> arrangments = 6*4=24
*/
}
//tells you the possible number of permutations possible for the word (24 for repeated letters i.e. considering "tttt" one word)
cout << "Your word has " << length << " letters, which can be arranged in " << arrangements << " different ways:" << endl << endl;
//PLEASE THOROUGHLY EXPLAIN WHAT IS HAPPENING FROM HERE DOWN**********************************************
int sum = 6, l = 0;
if (length == 4) {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (i != j)
{
for (int k = 0; k < 4; k++)
if ((k != i) && (k != j))
{
l = sum - (i + j + k);
cout << " " << word[i] << word[j] << word[k] << word[l] << endl;
}
}
}
else
{
cout << " INVALID STRING. LENGTH OF STRING MUST BE 4 LETTERS ONLY" << endl;
//output error statement if input word is more than four letters
}
cout << endl;
return 0;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started