A fresher, cleaner version morphed per your tips and my original outline
Hello Coder Head and Laserlight,
All of your suggestions have been great to learn from.
For the record, this practice project does include outputting
all of the letters of the alphabet, matches and no matches.
I am attaching the finished code. I am going to submit another version using the ASCII table and some new member functions.
In the meantime, here it is with a nice definition of strlen commented in:
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <Windows.h>
using namespace std;
bool checkBuffer( char buffArray[], int buffSize );
void parseBuffer( char alphaArray[], char buffArray[], int alphaSize, int buffSize );
int main()
{
char alphabetArray[]= "abcdefghijklmnopqrstuvwxyz";
char buffer[11];
cout << "Enter the string up to 10 letters." << endl;
cin.get(buffer, strlen(buffer) );
checkBuffer(buffer, strlen(buffer) );
parseBuffer( alphabetArray, buffer, strlen(alphabetArray), strlen(buffer) );
// www.cplusplus.com definition of strlen:
//char mystr[100]="test string"; defines an array of characters with a size of 100 chars,_
// but the C string with which "mystr" has been initialized has a length of only 11 characters._
// Therefore, while "sizeof(mystr)" evaluates to 100, "strlen(mystr)" returns 11.
system("Pause");
return 0;
}
bool checkBuffer( char buffArray[], int buffSize )
{
if(buffArray, strlen(buffArray) == 0)
{
cout << "The buffer is empty. The program will end in 3 seconds. " << endl;
Sleep(3000);
}
exit(1);
}
void parseBuffer( char alphaArray[], char buffArray[], int sizeOne, int sizeTwo )
{
int countFreq = 0;
for(int i = 0; i < strlen(alphaArray); i++ )
{
countFreq = 0;
for(int j = 0; j < strlen(buffArray); j++)
{
if( alphaArray[i] == buffArray[j] )
countFreq = countFreq + 1;
}
cout << "The letter " << alphaArray[i] << " matched " << countFreq << " times." << endl;
}
}
-----------------------
OUTPUT:
Enter the string up to 10 letters.
itsahwzome
The letter a matched 1 times.
The letter b matched 0 times.
The letter c matched 0 times.
The letter d matched 0 times.
The letter e matched 1 times.
The letter f matched 0 times.
The letter g matched 0 times.
The letter h matched 1 times.
The letter i matched 1 times.
The letter j matched 0 times.
The letter k matched 0 times.
The letter l matched 0 times.
The letter m matched 1 times.
The letter n matched 0 times.
The letter o matched 1 times.
The letter p matched 0 times.
The letter q matched 0 times.
The letter r matched 0 times.
The letter s matched 1 times.
The letter t matched 1 times.
The letter u matched 0 times.
The letter v matched 0 times.
The letter w matched 1 times.
The letter x matched 0 times.
The letter y matched 0 times.
The letter z matched 1 times.
Press any key to continue . . .