Although it is correct, instead of writing this:
Code:
char alphabetArray[]={'a','b','c','d','e','f','g','h','i','j','k',
'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
Write the more concise version:
Code:
char alphabetArray[] = "abcdefghijklmnopqrstuvwxyz";
Next, note that when used on an array, the sizeof operator results in the number of bytes of the elements of the array. alphabetArray actually contains 27 elements, each of one byte. As such, your loop that loops through alphabetArray also accesses alphabetArray[26], which is a null character. It probably does not make sense to include that, so it would be better to use strlen instead of sizeof (e.g., call strlen(alphabetArray) once just before the loop, then use the return value), or to subtract one.
As a matter of style, we would typically write this:
Code:
countFreq = countFreq + 1;
as:
Finally, it is good to see that you have used indentation to show the structure of your code, but you could be a little more consistent, e.g.,
Code:
// loop through alphabet array
for(int i = 0; i < sizeof(alphabetArray); )
{
cout << "The letter " << alphabetArray[i] << " matched " << countFreq << " times." << endl;
i++;
countFreq = 0;
for(int j = 0; j < sizeof(buffer); j++)
{
if( alphabetArray[i] == buffer[j] )
countFreq = countFreq + 1;
}
}
could have been:
Code:
// loop through alphabet array
for(int i = 0; i < sizeof(alphabetArray); )
{
cout << "The letter " << alphabetArray[i] << " matched " << countFreq << " times." << endl;
i++;
countFreq = 0;
for(int j = 0; j < sizeof(buffer); j++)
{
if( alphabetArray[i] == buffer[j] )
countFreq = countFreq + 1;
}
}