views:

174

answers:

5

This is for homework! But I need help anyway. The assignment is to input a sentence then output the number of words, and the number of occurrences of each letter. The output must have the letters in alphabetical order. So far, I've been able to count the number of words and get all the letters to lower case so that I'll be able to keep count of them. My question is how to actually keep count of the letters. Example of output: "I say Hi."

3 words 1 a 1 h 2 i 1 s 1 y

Here's the code that I have so far:

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    int letters[26];
    char letter;
    int word = 0;

    cout << "Please enter a sentence: "<< endl;

    do 
      {
     cin.get(letter);
     if(isspace(letter))
        word++;

     letter = tolower(letter);
     cout << letter;

      }
    while (letter != '\n');


cout << "The number of words = " << word << endl;

return 0;

}

Should I input directly into a C-string? or will that mess up the word count? Any help is greatly appreciated!!!! Thanks ya'll, Mage Wyntyr

+3  A: 

If you're allowed to use STL, use std::map for mapping letters to counters. It will additionally sort the letters.

Otherwise, treat chars as indexes in an array of counters and increment them.

Dmitry
std::map is over doing it. You don't need to set up an entire red-black tree with 26 nodes when a simple contiguous array will do fine.
Charles Salvia
A: 

you didn't even google... that lazy?

just somebody
This isn't an answer. There's a reason that there's a comment section.
rlbond
comments generally require 50 karma (check the FAQ). this was my second post here, and my karma was 11.
just somebody
A: 

Hint: tolower(letter)-'a' is:

0 if letter is a
1 if letter is b
...
Stefan Mai
A: 

Hm, just few points to make your home task more useful to you (and your code more correct):

  • Think what happens if you have file with several spaces in a row (word counting).
  • Think how to be more correct with 'letters' (check for isalpha() at least). Also isalpha() could be key for simpler counting with fixed array [256] (this might be even the best solution as for performance vs std::map usage, check std::map documentation anyway).
  • Think about more effective file input. At least line at once.
Roman Nikitchenko
+3  A: 

My question is how to actually keep count of the letters

It's fairly straight forward. Simply create an array of 26 integers, (one for each letter), and initialize it to zero.

int letters[26] = { 0 }; // Initialize array to zero

Each value in the array corresponds to a count of a particular letter. Array index 0 refers to 'a', array index 1 refers to 'b', and so on. Then, everytime you encounter a letter, increment the appropriate value in the array. You can use the character 'a' (ASCII value 97) as a starting offset. So, given the variable char letter; you would do:

++letters[tolower(letter) - 'a'];

But always make sure that before you increment the appropriate value in the array, you check that isalpha(letter) && islower(letter) to make sure that your letter is in the range of lowercase a-z; otherwise you will access an index beyond the bounds of the array. You can also test for this condition by saying if (letter >= 'a' && letter <= 'z').

Charles Salvia
Exactly what I was going to say. char's are just unsigned ints that are one byte in length (which is usually 8 bits), so you can perform any integer operations on them. If you're not familiar with what an ASCII table is, google it, should make more sense then.
Nali4Freedom