4. Write a function for c++ that accepts a word as an input argument (type strin
ID: 3734861 • Letter: 4
Question
4. Write a function for c++ that accepts a word as an input argument (type string) and generates several different scrambles of the word. (Output to the screen.) The input word can be from 4 to 10 letters in length. The number of scrambles produced depends on the number of letters in the word. e.g. the 4 letter word “lose” would have 4 different scrambles produced and the seven letter word “edition” would have seven different scrambles.
Here is a candidate example: Input: FLOOR
Possible Scrambles: ROOLF, OOLFR, OLFRO, LOORF, OORFL
Your function must use the same block of code for each input word. (That is you cannot test for the word length and then have special code depending on the result.) You have a number of tools available to assist with this including the reverse string operation you did in homework three. There is also the rand() random number generator available. Come up with your own algorithm for this problem. PLEASE DO NOT USE VECTOR NOTATION.
Explanation / Answer
Answer:
scarmble.cpp:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int n;
char strScamble[n];
char temp;
cout<<"Please Enter the word of length 4-10 characters: ";
cin>>str;
n = str.length();
// Copying string to Char array
for(int i=0; i<n; i++){
strScamble[i] = str.at(i);
}
cout<<"The possible scambeles are: "<<endl;
for(int x,j,i =0; i<n; i++){// loop for scrambling
temp = strScamble[0];
for( j=0; j<n-1; j++){
strScamble[j] = strScamble[j+1];
}
strScamble[j] = temp;
// Loop for Displaying the possible scrambles
for(x=0; x<n; x++){
cout<<strScamble[x];
}
cout<<endl;
}
return 0;
}
Output_1:
Please Enter the word of length 4-10 characters: HELLO
ELLOH
LLOHE
LOHEL
OHELL
HELLO
Output_2:
Please Enter the word of length 4-10 characters: MYWORLD
The possible scambeles are:
YWORLDM
WORLDMY
ORLDMYW
RLDMYWO
LDMYWOR
DMYWORL
MYWORLD
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.