Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <conio.h>
#include<stdlib.h>
using namespace std;
//
// Structure to hold a letter and the number of times it has occurred
//
const int MAX_LETTERS = 26 ;
struct letterCount
{
char letter;
int count;
int count_caps; // number of capital occurrences
};
int static openFile (FILE*& in ,FILE*& ot) ;
void static count (letterCount* a, FILE*& fin, int& td, int& w_num, unsigned int& line) ;
void static printResult (letterCount a[], FILE*& out, int total_digits, int w_num, unsigned int& line) ;
int main()
{
letterCount counts[MAX_LETTERS];
int total_digits ;
int w_num ;
unsigned int line;
FILE* in;
FILE* out;
memset (counts, 0x00, sizeof(counts)) ;
if ( 0 > (openFile (in , out)))
{
getch() ;
return -1 ;
}
total_digits = 0 ;
w_num = 0;
line = 0 ;
count (counts, in, total_digits, w_num, line) ;
printResult (counts, out, total_digits, w_num, line) ;
getch() ;
return 0 ;
}
printf("The Input file has %d words\n", w_num);
fprintf(out, "\nThe Input file has %d words\n", w_num);
printf("The Input file has %d lines\n", line);
fprintf(out, "\nThe Input file has %d lines\n", line);
void static count (letterCount* a, FILE*& fin, int& td, int& w_num, unsigned int& line)
{
char ch, ch1;
unsigned int i=0, minLine=0, minChars=65535, maxLine=0, maxChars=0, totChars=0;
int w_state ; // 0 - not-word; 1-word;
line = 0 ;
td = 0 ; // total number of digits
w_num = 0 ;
w_state = 0 ; // idle, looking for word start
i = 0;
ch = fgetc(fin);
while (ch != EOF){ /* Read until EOF is reached */
if (isalnum (ch))
{
if (0 == w_state)
{
w_state = 1 ; // started word
}
}
else
{
if (1 == w_state)
{
w_state = 0 ; // idle, looking for word start
w_num++ ;
}
}
if (isalpha (ch))
{
int up = isupper (ch) ? 1 : 0 ;
ch1 = ch ;
int idx = tolower(ch1) - 'a' ;
a [idx].count++ ;
a [idx].letter = ch1 ;
a [idx].count_caps += up ;
}
else
{
if (isdigit (ch))
{
td++ ;
}
}
i++; /* Count the characters*/
ch = fgetc(fin);
if ( ch=='\n'){
line++; /* increment when a newline is found*/
totChars += i-1; /* Correction for \n (newline) character*/
if (i<minChars){
minChars = i;
minLine = line;
}
else if (i>maxChars || minChars>maxChars){
maxChars = i;
maxLine = line;
}
i = 0;
}
}
if (i > 0) // last line may not have \n (newline) character
{
line++; /* increment when a newline is found*/
totChars += i; /* Note, no \n (newline) character*/
if (1 == w_state)
w_num++
;
if (i<minChars){
minChars = i;
minLine = line;
}
else if (i>maxChars || minChars>maxChars){
maxChars = i;
maxLine = line;
}
i = 0;
}
/* The file has been read */
// printf("\nThe Input file has %d lines\n", line);
int static openFile (FILE*& in ,FILE*& out)
{
char name [256];
cout << "Enter input file name:" << endl ;
cin.getline(name, 255);
in = fopen(name,"r");
if (!in)
{ // printf ("File \"input.txt\" was not opened!\n") ;
cout << "File " << name << " was not found!" << endl ;
return -1 ;
}
cout << "Enter output file name:" << endl ;
cin.getline(name, 255);
out = fopen(name,"w");
if (!out)
{
cout << "File " << name << " could not be created!" << endl ;
return -1 ;
}
}