Need this problem written in C++ with detailed documentation. Name of text file
ID: 3591626 • Letter: N
Question
Need this problem written in C++ with detailed documentation. Name of text file to be used is dwords.txt
The contents of that file are written below
The Diamond D and the Dreadful Dragon
Dozens of years ago. In a drafty castle. Duke David of
Dundeedle did dwell. Duke David was dumpy but dignified.
And he had a darling daughter named Dora who was a delight.
One day Dora danced through the door in the dear little dress
decorated with daffodils. "Doodley-doo, doodley-doo", Dora
sang, as she danced. "Oh, hello, dear, dumpy daddy", said
Dora to Duke David of Dundeedle.
"Dora, my darling, dimpled daughter", said Duke David. "You
are indeed delightful, so I have a dandy present for you."
"Do tell," said Dora. "Do describe this doo-dad, daddy".
"It is a dazzling Diamond D dangling from a chain!" So,
hanging the D around Dora's dimpled neck, Duke David of
Dundeedle departed through the door.
Although Dora and Duke David didn't know it, someone else
dwelt in the castle. This someone was Donald, the
Dreadful Dragon of Dundeedle.
"I am Donald, the Dreadful Dragon of Dundeedle. I live in a
dungeon downstairs in Duke David's dwelling. It is a deep
dungeon, a dark dungeon - a deep, dark, damp, dank, dreary
dungeon. It is a dump!"
Donald used dozens of D words - which was the only nice thing
you could say about him. Otherwise he was a dud.
Donald the Dragon dashed from his dungeon, directly to
Duke David's dandy daisy patch. "I am dashing from my dungeon
to steal the Diamond D that Duke David gave his daughter Dora,"
he said.
Donald, the doer of dark deeds, drifted into the daisy patch. Dora,
daughter of duke David, saw the dreadful dragon and dropped a drooping
daisy from her dainty dimpled hand. She danced directly up to Donald the
Dragon and declared, "Well, look who's here - a dear doggie! Hi there,
doggie."
Donald disbelieved his droopy ears. "Doggie?" he screamed. "Did
you say doggie?"
"Definitely," said Dora.
"Well, I do declare!" said Donald. "If Dora, the dignified Duke David's
daughter thinks that I, Donald the Dreadful Dragon of Dundeedle, am a
doggie, then Dora is dumb!"
"But you're the dearest doggie I've ever seen," Dora declared. "Do come
and dwell in our castle, doggie dear, and I'll give you this Diamond
D which my doting daddy draped around my dimpled neck."
"Don't doggie me!" Donald roared, darting toward Dora, determined to grab
the Diamond D.
Suddenly, Duke David dashed into the daisy patch. Diving between
darling Dora and Dreadful Donald, the Duke drove Donald around and
around the daisy patch. The dismal dragon ducked, dodged, darted
and dashed through the ducklings and the daisies until the
Duke dumped him into the duck pond.
"Daddy dear," said Dora, "why did you do that?"
"Because that dreadful, dishonest dragon was determined to steal the
dazzling Diamond D from around your dainty, dimpled neck," said the
Duke.
"Dragon?" said Dora. "I'll be darned! He looked like a doggie to
me."
"You're dumb but you're adorable, Dora," said Duke David, "so here's
what we'll do. From this day on this dreadful dragon will dwell in
a dog house, dine on dog biscuits, and do doggie tricks to delight my
divine daughter Dora - or else!"
"Or else what?" Donald demanded.
"I'll dump you in the duck pond again!"
"Bow-wow," said Donald. "I'm a doggie."
And so David, Duke of Dundeedle, and his delightful daughter Dora
lived happily ever after with Donald the Doggie, who used to be a
dreadful dragon.
Taken from The Sesame Street Story Book, Random House, 1971.
Explanation / Answer
#include <string>
#include <fstream>
#include <streambuf>
#include <iostream>
using namespace std;
int main ()
{
int arraysize = 100000;
char myArray[arraysize];
char current_char;
string file_name;
int num_characters = 0;
int i = 0, vowels = 0, consonants = 0;
// We are prompting the user to enter the file name
cout << "Enter the file name"<< endl;
// reading the file name entered by user and storing it into file_name variable
cin >> file_name;
// creating input file stream reference
ifstream myfile;
// opening the file name entered by user
myfile.open(file_name.c_str());
// show error if file is not readable or doesn't exits
if (!myfile) {
cout << "File Cannot be opened";
return -1;
}
// copy the contents of the file into a string variable
string str((istreambuf_iterator<char>(myfile)),istreambuf_iterator<char>());
// iterate trough each letter of the contents of the file and increase the count of vowels and consonants based of the if condition
// here we using vowels counter in order to reduce th search condition for consonants or else we can directly utilize if condition composed of consonants
for(int i = 0; i < str.size(); i ++){
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')
{
vowels = vowels + 1; // this statement is optional
}
else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
{
consonants = consonants + 1;
}
}
cout << endl;
// display the count of consonants in the file
cout << "Total Number of Consonants in "<< file_name <<" is " << consonants << endl;
myfile.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.