Below is a script that completes the following: Perform 4-letter WORD UNSCRAMBLI
ID: 3906297 • Letter: B
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 'TEST' can be unscrambled as TEST, TETS, TSET, TSTE, TTSE, TTES, etc.
#include "stdafx.h"
#include <iostream>
#include <string>
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;
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <string>
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**********************************************
//scrambled word will be word[i] + word[j] + word[k] + word[l], where i, j, k ad l are distinct.
//As word size is 4. indices will be 0, 1, 2, 3. so at most sum of them will be 6. l is fourth index variable.
int sum = 6, l = 0;
//if word length is 4
if (length == 4) {
for (int i = 0; i < 4; i++) //iterate i (first index) from 0 to 3
for (int j = 0; j < 4; j++) //iterate j (second index) from 0 to 3
if (i != j) //if i and j are not same
{
for (int k = 0; k < 4; k++) //iterate k (third index) from 0 to 3
if ((k != i) && (k != j)) //k is equals to i and j
{
l = sum - (i + j + k); //get fourth inde l by subtracting i+j+k from sum(which is 6)
cout << " " << word[i] << word[j] << word[k] << word[l] << endl; //print scrambled word
}
}
}
else //if word length is not 4
{
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.