# Assignment 5

```Prof. I Rudowsky
Spring 2008
CIS1.5 MW12
Assignment 5
Due date: April 30, 2008
This homework involves reading sentences from a file and determining the makeup of the sentences,
words and characters.
1. First, you will read in the sentences with the function
This will read in the sentences from a file named Sentences.txt (see below for the input)
Each line ends with a blank space after the last letter. This is necessary to make sure you get
all the words correctly.
The function will fill up an array named sentences and set a variable named numSentences (which is
passed by reference), which will be used in later functions as well. The function will also print out each
sentence as it is read in.
2. The next line in main calls the function countNumWords(string sentences[], int numSentences)
which will count how many words there are in all the sentences. Remember that there is a blank space
after each word.
3. Once the sentences are in the array sentences, call the function
countAlphaChars(string sentences[], int numSentences, int letters[])
which will go through each sentence, character by character, find where each letter is in the global
string alphabet and use that position as the index for the array letters which then be increased by 1.
Thus, letters[0] represents the letter A. Whenever an A is encountered in a sentence, letters[0] is
increased by 1. We use an index of 0 because in the string alphabet, the letter A is in position 0.
4. Once the array letters has been populated, the function printLetters(int letters[]) will be called to
print each letter and how many there were in all the sentences. Remember that letter [i] corresponds
to alphabet[i] so you can first print the letter and then the count in the same loop. Skip any letters
that have a count of 0.
5. Next the function
extractWords(int wordCount[], string words[], string sentences[],
int numSentences, int &amp;wordsInList)
is called. This function goes through the sentences, extracts the words (use the substr string
function)one at a time. When a word is extracted, the array words is checked to see if the word is in
the array already. This can be done by looping through the array and comparing word[i] with the
extracted word to see if they are equal. The number of words in the list is stored in wordsInList which
is passed by reference to the function and initialized to 0. If there is no match, then set
words[wordsInList] to that word, set wordCount[wordsInList] to 1 and finally increase
wordsInList by 1. If the word is found in words[i] then just increase wordCount[i] by 1.
6. Finally, the function printWords(string words[], int wordCount[], int wordsInList)
is called to print the words stored in the array words and how many there are of each word from the
array wordCount. The variable wordsInList has the actual number of words stored.
PROGRAM OUTLINE:
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
using namespace std;
void
void
int
void
void
void
countAlphaChars(string[], int, int[]);
countNumWords(string[], int);
extractWords( int[], string[], string[], int, int &amp;);
printWords(string[], int [], int);
printLetters(int[]);
ifstream infile;
string alphabet=&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&quot;;
int main()
{
string sentences[10];
int numSentences=0;
int wordsInList=0;
int letters[52] = {0}; //quick way to initialize the entire array to 0
int wordCount[100] = {0};
string words[100];
infile.open(&quot;C:\\Sentences.txt&quot;);
cout&lt;&lt;&quot;The number of words is &quot;&lt;&lt;countNumWords(sentences,numSentences)&lt;&lt;endl&lt;&lt;endl;
countAlphaChars(sentences, numSentences, letters);
printLetters(letters);
extractWords(wordCount, words, sentences, numSentences, wordsInList);
printWords(words, wordCount, wordsInList);
infile.close();
system (&quot;pause&quot;);
return 0;
}
}
void printLetters(int letters[]){
}
int countNumWords(string sentences[], int numSentences, char c){
}
void countAlphaChars(string sentences[], int numSentences, int letters[]){
}
void extractWords(int wordCount[], string words[], string sentences[], int numSentences, int
&amp;wordsInList){
}
void printWords(string words[], int wordCount[], int wordsInList){
}
Sentences.txt
The number of words in a sentence is to be counted
Each sentence ends with a period
Each word is followed by a space or period
OUTPUT:
sentence 0: The number of words in a sentence is to be counted
sentence 1: Each sentence ends with a blank space
sentence 2: Each word is followed by a one space
There are 3 sentences
The number of words is 27
Letter
E
T
a
b
c
d
e
f
h
i
k
l
m
n
o
p
r
s
t
u
w
y
Word
The
number
of
words
in
a
sentence
is
to
be
counted
Each
ends
with
blank
space
word
followed
by
one
Count
2
1
8
4
7
5
15
2
4
4
1
3
1
10
8
2
3
8
5
2
4
1
Count
1
1
1
1
1
3
2
2
1
1
1
2
1
1
1
2
1
1
1
1
```