Write a program in C++ that reads the data from a text file and performs the fol
ID: 3851677 • Letter: W
Question
Write a program in C++ that reads the data from a text file and performs the following calculations and displays it on the screen. Note that, you should make use of pointers and character arrays for referencing your text. All the manipulations on the text should be done using pointers and should not make use of any ‘C’ String library functions.
Your program should prompt the user for a file name to process, read the text file, and display the following:
Number of words in it. A word is basically delimited by a space or a tab. (Note: consecutive spaces or tabs should be treated as one)
Number of characters in the file.
Number of the capitalized characters.
Number of the letters.
Number of digits.
Number of special characters.
Number of lines in the document
Explanation / Answer
#include<iostream.h>
#include<fstream.h>
int main()
{
ifstream data("samp.txt"); //opening text file
int line=1,word=1,size; //will not count first word and last line so initial value is 1
char ch;
data.seekg(0,ios::end); //bring file pointer position to end of file
size=data.tellg(); //count number of bytes till current postion for file pointer
data.seekg(0,ios::beg); //bring position of file pointer to begining of file
while(data)
{
data.get(ch);
if(ch==' '||ch=='n')
word++;
if(ch=='n')
line++;
data.get(ch);
i=ch;
if(i>63&&i<91||i>96&&i<123)
a++;
else
if(ch==’ ‘)
s++;
else
if(i>47&&i<58)
d++;
}
cout<<“No. OF Alphabates:”<<a;
cout<<“nNo. Of Digits:”<<d;
cout<<“nNo. Of Spaces:”<<s;
cout<<"Lines="<<line<<"nWords="<<word<<"nSize="<<size<<"n";
data.close(); //closing file
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.