Read in string and evaluate for word count and letter occurrences

Read in string and evaluate for word count and letter occurrences

LANGUAGE: C++

CHALLENGE:

Write a program that will read in a line of text and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either a whitespace, a period, a comma, or the beginning or end of the line.

SOLUTION:



#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int CountWords(string strString);
char add_letter( char letter, int counters[] );

int main(){
	std::string input;
	int counters[26] = { 0 };

	//request String
	cout << "Please enter a sentence.\n";
	getline(cin, input);

	//Display Word Count
	cout << "\n" << CountWords(input) <<  " Words" << endl;

	char *sentence=new char[input.size()+1];
	sentence[input.size()]=0;
	memcpy(sentence,input.c_str(),input.size());

	//Count Letters
	while ( add_letter(*sentence++, counters) );

	//Display Letter Results
	for (int i = 0; i < 26; ++i) {
		if ( counters[i] ) {
			cout << counters[i] << ": " << char(i + 'a') << '\n';
		}
	}

	cout << "\nPress enter to exit. ";
	cin.get();
	return 0;
}

int CountWords(string strString){
	int nSpaces = 0;
	unsigned int i = 0;

	while(isspace(strString.at(i)))
		i++;

		for(; i < strString.length(); i++){
			if(isspace(strString.at(i))){
				nSpaces++;

				while(isspace(strString.at(i++))){
					if(strString.at(i) == '\0'){
						nSpaces--;
					}
				}
			}
		}

	return nSpaces + 1;
}

char add_letter( char letter, int counters[] ) {
	char lower_case_letter = tolower(letter);
	if ( lower_case_letter >= 'a' && lower_case_letter <= 'z' ) {
		++counters[ lower_case_letter - 'a' ];
	}
	return letter;
}

Add comment