A file \"diction.txt\" contains a randomly-ordered list of words, one word per r
ID: 3575148 • Letter: A
Question
A file "diction.txt" contains a randomly-ordered list of words, one word per record. You can assume that the words consist entirely of small letter! ('a', 'b', ..., 'z'); no other symbols or characters are present (except for "enter" keystrokes or blank spaces separating one word from another). For char array declaration purposes, you can assume all words in the file are less than 80 characters in length. Write a C++ program which, for each small letter 'a' through 'z', calculates and outputs the length of the longest word in the file which begins with the letter (Don't worry about ties.) For example, for the very simple input file: four score and seven years ago our fathers brought forth on this continent Your output should consist of the 26 lines: maximal length of words beginning with a = 3 maximal length of words beginning with b = 7 maximal length of words beginning with c = 9 maximal length of words beginning with d = 0 maximal length of words beginning with e = 0 maximal length of words beginning with f = 7 ... ... ... ... ... ... ... maximal length of words beginning with y = 5 maximal length of words beginning with z = 0Explanation / Answer
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
int main()
{
//ofstream file;
//file.open("pro.txt");
ifstream fin;
fin.open("diction.txt");
char ch[50];
int count=0,i=0,no=0;
while(!fin.eof())
{
fin>>ch;
if(ch[0]=='a')
{
i=strlen(ch);
cout<<" the word begin wth a is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='b')
{
i=strlen(ch);
cout<<" the word begin wth b is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='c')
{
i=strlen(ch);
cout<<" the word begin wth c is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='d')
{
i=strlen(ch);
cout<<" the word begin wth d is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='e')
{
i=strlen(ch);
cout<<" the word begin wth e is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='f')
{
i=strlen(ch);
cout<<" the word begin wth f is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='g')
{
i=strlen(ch);
cout<<" the word begin wth g is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='h')
{
i=strlen(ch);
cout<<" the word begin wth h is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='i')
{
i=strlen(ch);
cout<<" the word begin wth i is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='j')
{
i=strlen(ch);
cout<<" the word begin wth j is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='k')
{
i=strlen(ch);
cout<<" the word begin wth k is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='l')
{
i=strlen(ch);
cout<<" the word begin wth l is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='m')
{
i=strlen(ch);
cout<<" the word begin wth m is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='n')
{
i=strlen(ch);
cout<<" the word begin wth n is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='o')
{
i=strlen(ch);
cout<<" the word begin wth o is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='p')
{
i=strlen(ch);
cout<<" the word begin wth p is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='q')
{
i=strlen(ch);
cout<<" the word begin wth q is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='r')
{
i=strlen(ch);
cout<<" the word begin wth r is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='s')
{
i=strlen(ch);
cout<<" the word begin wth s is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='t')
{
i=strlen(ch);
cout<<" the word begin wth t is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='u')
{
i=strlen(ch);
cout<<" the word begin wth u is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='v')
{
i=strlen(ch);
cout<<" the word begin wth v is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='w')
{
i=strlen(ch);
cout<<" the word begin wth w is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='x')
{
i=strlen(ch);
cout<<" the word begin wth x is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='y')
{
i=strlen(ch);
cout<<" the word begin wth y is:"<<ch<<" has len:"<<i;
}
if(ch[0]=='z')
{
i=strlen(ch);
cout<<" the word begin wth z is:"<<ch<<" has len:"<<i;
}
count++;
}
cout<<" Number of words in file are "<<count;
fin.close();
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.