include include include using namespace std string jumbleWor

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
string jumbleWord(string word)
{
int count = rand() % 5 ;
if(word.length() > 3)
for(int i = 0; i < count; i++)
{
int one = (rand() % (word.length()-1)) + 1;
int two = (rand() % (word.length()-1)) + 1;
char temp = word.at(one);
word.at(one) = word.at(two);
word.at(two) = temp;
}
return word;
}
int main()
{
ifstream fin;
ofstream fout;
string fileName, word;
cout<<\"Enter the name of the input file: \";
cin>>fileName;
fin.open(fileName);
cout<<\"Enter the name of the output file: \";
cin>>fileName;
fout.open(fileName);
while(!fin.eof())
{
fin>>word;
fout<<jumbleWord(word)<<\" \";

}
fin.close();
fout.close();
}

Solution

Please follow the code and comments for description :

CODE :

#include <iostream> // required header files
#include <cstdlib>
#include <fstream>

using namespace std;

string jumbleWord(string word) // function to junble the letters
{
   int count = rand() % 5 ; // get the random count
   if(word.length() > 3) // check for the length
   for(int i = 0; i < count; i++)
   {
       int one = (rand() % (word.length()-1)) + 1; // get two random numbers
       int two = (rand() % (word.length()-1)) + 1;
       char temp = word.at(one); // swap the letters
       word.at(one) = word.at(two);
       word.at(two) = temp;
   }
   return word; // return the letters
}

int main() // driver method
{
   ifstream fin; // local variables
   ofstream fout;
   string fileName, word;
   cout<<\"Enter the name of the input file: \"; // prompt for the user
   cin>>fileName; // get the data
   fin.open(fileName); // opoen the file
   cout<<\"Enter the name of the output file: \"; // message
   cin>>fileName;
   fout.open(fileName); // open the file name
   while(!fin.eof()) // check for the end of file
   {
       fin>>word; // read the word
       fout<<jumbleWord(word)<<\" \"; // call the function
   }
   fin.close(); // close the files
   fout.close();
}


OUTPUT :

main
Enter the name of the input file: input.txt
Enter the name of the output file: out.txt

input.txt :

I couldn\'t believe that I could actually understand what I was reading. Using the incredible power of the human brain, according to research at Cambridge University, it doesn\'t matter in what order the letters in a word are, the only important thing is that the first and last letter be in the right place. The rest can be a total, mess and you can read it without a problem. This is because the human mind does not read every letter by itself, but the word as a whole. Amazing, huh? Yeah and I always thought spelling was important! See if your friends can read this too!


out.txt :

I cou\'dnlt beivele that I could altualcy utderdnans wath I was rgndiae. Ugisn the iicrednble power of the human brain, agdornicc to resraech at Cimarbdeg Uveni,sytir it dtesno\' matter in wtha odrer the lettsre in a word are, the oyln impartont tihng is taht the first and lats lertet be in the rtghi place. The rets can be a tl,aot mess and you can reda it whtotui a problem. Tshi is busecae the hnmau mind dsoe not read eevry letter by iestlf, but the word as a whole. Amngaiz, hhu? Yhea and I awlasy thohugt sienlplg was important! See if yoru frisnde can read tihs to!o too!

Description :

The code successfully ran using the c++11 and therea re no compile time or run time errors. If still the error persists please do comment the error so as to look after it for the reason.

Hope this is helpful.

#include <iostream> #include <cstdlib> #include <fstream> using namespace std; string jumbleWord(string word) { int count = rand() % 5 ; if(wo
#include <iostream> #include <cstdlib> #include <fstream> using namespace std; string jumbleWord(string word) { int count = rand() % 5 ; if(wo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site