Subject: Object oriented programming Software: netbeans. Problem: Text processin
ID: 674685 • Letter: S
Question
Subject: Object oriented programming
Software: netbeans.
Problem: Text processing
-Program: reads given text; outputs the text as is; prints number of lines and number of times each letter appears in text
-Input: file containing text to be processed
-Output: file containing text, number of lines, number of times letter appears in text
*Solution hint:
-An array of 26 representing the letters in the alphabet
-Three methods:
copyText
characterCount
writeTotal
-Value in appropriate index incremented using methods and depending on character read from text
Explanation / Answer
SOLUTION:-
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
int lineCount = 0;
int letterCount[26];
for(int i = 0; i < 26; i++)
letterCount[i] = 0;
ifstream infile;
infile.open("textinput.txt", ios::in);
if(!infile)
{
cerr<<"File does not exist."<<endl;
exit(1);
}
ofstream outfile;
outfile.open("textoutput.txt", ios::out|ios::binary);
if(!outfile)
{
cerr<<"File cannot be opened."<<endl;
exit(1);
}
char data[100];
outfile<<data;
while(infile>>data)
{
outfile<<data<<endl;
}
while(infile)
{
char ch1 = infile.get();
if(ch1 == ' ')
{
lineCount++;
continue;
}
int asciiNum = (int)ch1;
if(asciiNum > 96)
{
asciiNum = asciiNum - 97;
}
else
{
asciiNum = asciiNum - 65;
}
letterCount[asciiNum]++;
}
infile.close();
outfile.close();
system("PAUSE");
return 0;
}