C++ please You are given a zipped file “Jan.zip” which contains the following fi
ID: 3912165 • Letter: C
Question
C++ please
You are given a zipped file “Jan.zip” which contains the following files:
? aol.html
? armed.html
? baptist.html
? bill.html
? birdnbee.html
? bunker.html
? cache.html
? child.html
? creditcard.html
? debug.html
? edwardii.html
? explain.html
? fab.html
? galant.html
? gravies.html
? harley.html
? heartprob.html
? hippos.html
? jesus.html
? kitty.html
? marriedplay.html
? phone.html
? problem.html
? qc.html
? quickies.html
? snow.html
? superbowl.html
? topten.html
? y2k.html
? y2kfollow.html
? y2kms.html
You need to write a program to do the following two parts:
? Part 1: Repeatedly read each of these files. From the content of each file, extract words
(or strings) that contain ONLY alphabets and save these words (or strings) in a list (or
some other data structure).
For examples, the words (strings) below shall not be extracted:
<title>
[rec.humor.funny]
type="text/css"
media="screen">
bgcolor="#ffffff"
text="#000000"
link="#0000ee"
vlink="#551a8b">
<!--
"/include/rhf/top.ofi"
-->
<map
name="joke-header-map">
shape="rect"
coords="103,52,194,71"
href="../../../best.html"
alt="best
jokes">
shape="rect"
coords="205,52,299,71"
href="../../../current.html"
alt="current
jokes">
href="../../../images/joke-header.html"><img
src="../../../images/joke-header.gif"
alt="fun
However, the words (strings) below must be extracted:
subject
provided
start
stuff
ismap
end
rhf
joke
archives
subject
much
was
hosting
survey
worst
music
videos
beating
poison
house
milli
vanilli
took
award
for
cheesiest
band
said
from
milli
vanilli
For ease of work, you shall convert all words (strings) into lower case.
? Part 2. Write a loop to do:
o Ask the user to enter a search key word save it at “searchKey”
o Look for this search key in extracted words (strings) of every file. If the file
contains the search key, then print “found a match” and display the name of the
file.
o If none of the files contains the search key, then print “No match”.
o The loop ends when an empty search key is entered.
For example, the following shows some iterations of the loop:
Now search begins:
enter a search key=> music
found a match: ./Jan/fab.html
enter a search key=> cat
no match
enter a search key=>
Explanation / Answer
#include<iostream>
#include<string.h>
#include <fstream>
#include <stdlib.h>
using namespace std;
//Data structure (List)
struct word_list
{
char word[20];
struct word_list *next;
};
struct file_name
{
char file_name[50];
struct word_list *wordhead;
struct file_name *next;
};
//Part:1
struct file_name * read_and_feed(char file[],struct file_name *file_name_head)//char *file
{
char filename[50];
int i=0;
char new_word[20];
ifstream infile;
char ch;
struct word_list *new1_word_list=NULL;
strcpy(filename,"./");
strcat(filename,file);
infile.open (filename);
if(infile.is_open())
{
struct file_name *new1_file_name=(struct file_name *)malloc(sizeof(struct file_name));
strcpy(new1_file_name->file_name,filename);
new1_file_name->wordhead=NULL;
while(infile >> noskipws >> ch)
{
if( (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || ch==' ' )
{
if(ch==' '&&(strcmp(new_word,"")!=0))
{
new1_word_list=(struct word_list *)malloc(sizeof(struct word_list));
strcpy(new1_word_list->word,new_word);
new1_word_list->next=new1_file_name->wordhead;
new1_file_name->wordhead=new1_word_list;
memset(new_word, '', sizeof(new_word));
i=0;
}
else
{
new_word[i]=ch;
i++;
}
}
else
{
memset(new_word, '', sizeof(new_word));
i=0;
}
}
new1_file_name->next=file_name_head;
file_name_head=new1_file_name;
}
else
{
cout<<" File not found";
}
return file_name_head;
}
//Part:2
int find(char key[],struct file_name *file_name_head)
{
int flag=0;
if(NULL!=file_name_head)
{
struct file_name *trav_file_name=file_name_head;
while(NULL!=trav_file_name)
{
struct word_list *trav_word_list=trav_file_name->wordhead;
while(NULL!=trav_word_list)
{
if(!strcmp(trav_word_list->word,key))
{
cout<<"Match found-"<<trav_file_name->file_name;
flag=1;
break;
}
trav_word_list=trav_word_list->next;
}
if(!flag)
{
trav_file_name=trav_file_name->next;
}
else
{
break;
}
}
}
else
{
cout<<" No data found";
}
return flag;
}
int main()
{
struct file_name *file_name_head=NULL;
char ch='y';
char filename[20];
char searchKey[20];
while(ch=='y'||ch=='Y')
{
cout<<" Enter file name:";
cin>>filename;
file_name_head=read_and_feed(filename,file_name_head); //call to Part:1
cout<<" Do you want to give one more file(Y/N OR y/n):";
cin>>ch;
}
while(1)
{
cout<<" Enter search key(E/e for Exit):";
cin>>searchKey;
if(!(strcmp(searchKey,"E")) || !(strcmp(searchKey,"e")))
{
break;
}
else
{
cout<<(strcmp(searchKey,"E")) || (strcmp(searchKey,"e"));
if(find(searchKey,file_name_head)) //call to Part:2
{
}
else
{
cout<<" No Match Found";
}
}
}
//To Print All Words
// if(NULL!=file_name_head)
// {
// struct file_name *trav_file_name=file_name_head;
// while(NULL!=trav_file_name)
// {
//
// struct word_list *trav_word_list=trav_file_name->wordhead;
// while(NULL!=trav_word_list)
// {
// cout<<" "<<trav_word_list->word;
// trav_word_list=trav_word_list->next;
// }
// trav_file_name=trav_file_name->next;
// }
// }
// else
// {
// cout<<" No data found";
// }
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.