Write a program that will look up a word provided by the user as an argument the
ID: 3770615 • Letter: W
Question
Write a program that will look up a word provided by the user as an argument then search and display the definition of the input word from the file “dictionary.txt” file (provided below). The dictionary.txt contains dictionary definitions, one per line, in the format ``word space definition''.
Hint: Use the three-argument form of split().
% cat dictionary.txt
delight Great pleasure or gratification; joy
deuteron The nucleus of a deuterium atom
distribution The act of distributing or the condition of being distributed
fiche A microfiche
karate A Japanese art of self-defense
rinse To wash lightly with water
rouge A red or pink cosmetic for coloring the cheeks or lips
slack Not lively or moving; sluggish
%
Explanation / Answer
You have not specified which language you want this program to be written in. But, inferring from your Hint to use the three-argument form of split(), I guess its perl. So, here is perl program to do so.
#!/usr/bin/perl
open(DATA,"<dictionary.txt") or die "Can't open the file";
@lines = <DATA>;
close(DATA);
my %dict;
foreach $line (@lines) {
my @word = split(/ /, $line, 2);
$dict{$word[0]} = $word[1];
}
print "Enter the word you want to look up in the dictionary: ";
my $input = <STDIN>;
chomp $input;
if( exists($dict{$input} ) ){
print "$input:- $dict{$input}";
}
else {
print "$input not found in the dictionary."
}
If it helped you, please rate this answer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.