I have made some progress on my current program and I am almost finished. I can't seem to get it to output the number of occuranences of each letter, however on my output screen I am able to get the code to output to the terminal. I am unsure as to why its displaying all 26 letters, and not counting or removing the ones that shouldn't be there, such as letters not in the sentence.
http://prntscr.com/9ady9w
Program description:
Write a program that will read in a line of text and output the number of words in the line and the number of occurences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, and periods. When outputting the number of letters that occur in a line, be sure to count upper and lowercase version of a letter as the same letter. Output the letter in alphabetical order and list only those letters that do occur in the input line. For example, the input line
I say Hi.
Should produce output similar to the following:
3 words
1 a
1 h
2 i
1 s
1 y
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <string>
using namespace std;
int Word_Counter(string string_Input);
void count(char[], int[]);
int main() {
string input;
char size[1000];
int counters[26] = {};
//request String
cout << "Please enter a sentence to analyze: \n";
cin >> size[1000];
getline(cin, input);
//Display Word Count
cout << "\n" << Word_Counter(input) << " Words" << endl;
count(size, counters);
for (int i = 0; i < 26; i++)
{
if (counters[i] = 0)
{
continue;
}
else {
cout << char(i + 'a') << ": " << counters[i] << " times\n";
}
}
cout << "\nPress enter to exit. ";
cin.get();
return 0;
}
int Word_Counter(string string_Input) {
int numSpaces = 0;
int i = 0;
// skips space at beginning of each word
while (isspace(string_Input.at(i)))
i++;
for (; i < string_Input.length(); i++) {
if (isspace(string_Input.at(i))) {
numSpaces++;
while (isspace(string_Input.at(i++))) {
// if null character is found (end of string) stop count
if (string_Input.at(i) == '\0') {
numSpaces--;
}
}
}
}
// number of words + 1 (c string)
return numSpaces + 1;
}
void count(char size[], int counters[])
{
for (int i = 0; i < 26; i++)
{
counters[i] = 0;
}
for (int i = 0; i < 1000; i++)
{
{
size[i] = tolower(size[i]);
counters[size[i] - 'a'] ++;
}
}
}