1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include<iostream>
#include<cctype>
#include<cstring>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<iomanip>
#include<string.h>
using namespace std;
//single structure that contains all functions + the line itself
struct Counts
{
int countChars;
int countWords;
int countSentences;
};
//function names
int countChars(Counts&, char[]);
int countWords(Counts&, char[]);
int countSentences(Counts&, char[]);
void printRprt(Counts&);
int main()
{
//open input file (C-style)
FILE *fp;
fp = fopen("inp1.txt", "r");
//error message
if(fp == NULL)
{
printf("Could not open input file\n");
exit(0);
}
//declare variables
Counts counts;
int i;
char line[80];
char delims[] = " ,\t\n"; //delimeters: space, ',', tab, return
//get lines (max 80) from file
//a line is a group of characters terminated by a \n
//Call to the functions
while(fgets(line, 80, fp))
{
printf(line);
countChars(counts, line);
countWords(counts, line);
countSentences(counts, line);
printRprt(counts);
}
}
/*
* Function name: countChars
* Output: An integer
* Description: Counts how many characters are on the given line.
*/
int countChars(Counts& counts, char line[])
{
counts.countChars=0;
int i=0;
while (line[i] !='\0')
{
{
counts.countChars++;
}
i++;
}
}
int countWords(Counts& counts, char line[])
{
counts.countWords=0;
char c;
int i=0;
while (line[i]!='\0')
{
c= line[i];
if (isspace(c))
{
counts.countWords++;
}
i++;
}
}
/*
int countWords(Counts& counts, char line[])
{
counts.countWords=1;
int i=0;
char c;
while (line[i]!='\0')
{
if(c == ' ')
{
counts.countWords++;
}
}
i++;
}*/
int countSentences(Counts& counts, char line[])
{
counts.countSentences=0;
int i=0;
char p=0;
while (line[i] !='\0')
{
if((p == '.')||(p == '?')||(p == '!'))
{
counts.countSentences++;
}
}
i++;
}
/*
* Function name: printReport
* Output: Results of all the functions.
* Description: Prints a report of all the functions.
*/
void printRprt(Counts& counts)
{
cout <<"Total characters: " << counts.countChars <<"\nTotal words: " << counts.countWords<< "\nPunctuation:" <<endl;// counts.countSentence<< endl;
}
|