#include <iostream>
using namespace std;

// Function to increment the letter count in the counters array
char add_letter( char letter, int counters[] ) {
  // Only deal with lower case letters
  char lower_case_letter = tolower(letter);

  // Check if character is a letter
  if ( lower_case_letter >= 'a' && lower_case_letter <= 'z' ) {
    ++counters[ lower_case_letter - 'a' ];
  }

  return letter;
}

int main() {
  // One integer for each  letter
  int counters[26] = { 0 };

  // Counting the number of occurences of each letter in this sentence
  char *sentence = "the quick brown fox jumps over the lazy dog";

  // Pass each character to the add_letter
  // function until null-terminator is reached
  while ( add_letter(*sentence++, counters) );

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

  cin.ignore();
}
3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by William Hemsworth
0

Well, you could use the isalpha function, but i'm also using the character as an index for an array, so it's just as simple to do it this way.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.