Sentence
A.First Edition
This is the first edition of my assignment and I don't think there will be any further edition
1¡£ Basic idea: Simply ask user to input a sentence ended with '.','!' or '?' and count the letters of each word
to give some statistic data for words.
2¡£ Program design: Quit simple.¡¡
3¡£ Major function£º
A. int inputSentence()
Major function for inputting and counting for letters and words. Noticed that the last ending mark will be excluded
from counting. And a checking for empty input is a must, otherwise calculating will have dividing 0 error.
B. int countWord(int& charNum)
Simply counting, indicating ending by return -1.
¡¡
4¡£ Further improvement£º
A. No.
/******************************************************* course: comp248 assignment no.2 section: X instructor: Ms Leila Kosseim program title: sentence author: Qingzhe Huang ID: 5037735 purpose of program: using program control special points: the last word will exclude the ending mark '.','?','!' *///////////////////////////////////////////////////////
#include <iostream>
//initializing number for minWord which means the max length of a word const int MaxLength =100; //initializing number for maxWord which means the min length of a word const int MinLength = 0;
using namespace std;
//this will count each word for one sentence int countWord(int& charNum);
//this will display result void display(int wordCount, int minWord, int maxWord, int totalChar);
//this allows user input one complete sentence int inputSentence();
int main() {
inputSentence(); //analyse sentence!
return 0; }
//this allows user input one complete sentence int inputSentence() { //record total number of character of sentence excluding last mark int totalChar=0, //record the number of char of longest word maxWord= MinLength, //record the number of char of shortest word minWord= MaxLength, //record number of words in sentence wordCount=0, //a temp variable for length of each word,passing as param tempCount =0, //testing the return value of function countWord test =0;
cout<<"Please enter the sentence to analyse" <<"(ended with a '.', '?' or '!')"<<endl;
while (true) { test = countWord(tempCount); //continue test word totalChar += tempCount; //inc total num of char in sentence if (tempCount!=0) //this means there is a new word { wordCount++; //inc word number if (tempCount>maxWord) //if found a longer word { maxWord = tempCount; //update number of char of longest word }
if (tempCount < minWord)//if found a shorter word { minWord = tempCount;//update num of char of shortest word }
} if (test==-1) //this means the end of sentence is reached and break break; }
if (wordCount!=0) //must do this, otherwise, there will be 0 as divident { display(wordCount, minWord, maxWord, totalChar); //call sub to display } else { cout<<"\nYou have not input a single word!"; } return wordCount; }
//this will count each word for one sentence int countWord(int& charNum) { char ch;
charNum =0; //initialize
cin.get(ch); while (ch!= '.'&& ch!='?'&&ch!='!'&&ch!=32) { charNum++; cin.get(ch); } if (ch==32) { return 1;//this means there is blank space after a word } //or simply a blank space itself else { return -1; //this means it is ending mark terminates the loop } }
//this will display result void display(int wordCount, int minWord, int maxWord, int totalChar) { cout<<"\nThere are "<<wordCount<<" words."; cout<<"\nThe longest word has "<<maxWord<<" characters."; cout<<"\nThe shortest word has "<<minWord<<" characters."; cout<<"\nThe average length is "<<(double)totalChar/wordCount <<" characters."; }