Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using std::cout;
- using std::cin;
- using std::endl;
- #include <string>
- using std::string;
- using std::getline;
- int letters[26]; //global variables - two arrays
- int wordLength[10];
- void countLetters(string str); //function prototypes
- void countWords(string str);
- string toUpperCase(string str);
- void printTables();
- int main() { //main function where user enters phrase
- string line;
- cout<<"Enter a phrase: ";
- getline(cin, line);
- line = toUpperCase(line);
- countLetters(line);
- countWords(line);
- printTables();
- system("PAUSE");
- return 0;
- }
- //function declarations
- void countLetters(string line){
- //to initialize all the items inside table to zero;
- for(int i=0; i<26; i++){
- letters[i] = 0;
- }
- int Ais65 = 65; //ASCII code of capital A is 65
- for(int i=0; i<line.length(); i++)
- {
- if(isalpha(line[i]))
- {
- int alphaNumber = line[i];
- alphaNumber = alphaNumber - 65;
- letters[alphaNumber]++; //we add one to number of occurrences of each letter of the alphabet passed through the program
- }
- }
- }
- void countWords(string str)
- {
- for(int i=0; i<10; i++){
- wordLength[i] = 0;
- }
- int length = 0;
- for(int i = 0; i < str.length(); i++){//counts the number of words of each length-type
- if(isalpha(str[i])){
- length++;
- }
- else{
- if(length != 0 && length < 11){ //resets word length for next word
- wordLength[length-1]++;
- length = 0;
- }
- }
- }
- //just to make sure we get the last word
- if(length != 0 && length < 11){
- wordLength[length-1]++;
- }
- }
- string toUpperCase(string str){ //making the user-entered phrase into all capital letters
- string tmp = "";
- for(int i=0; i<str.length(); i++)
- {
- tmp += toupper(str[i]);
- }
- return tmp;
- }
- void printTables(){ //prints the tables for the program
- cout<<"Letter \t Number of Occurrences\n";
- char ch;
- for(int i = 0; i < 26; i++){
- ch = i + 65;
- cout<<" "<<ch<<" "<<letters[i]<<endl;
- }
- cout<<"Word Length \t Occurrences\n";
- for(int i = 0; i < 10; i++){
- cout<<" "<<i+1<<" "<<wordLength[i]<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement