Patrick B. answered 04/17/20
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#define MAX_WORD_LEN (255)
#include <string.h>
int LinearSearch( char * token, char A[][MAX_WORD_LEN],int N)
{
int iReturn=-1;
//cout << " Searching for " << token << endl;
for (int iLoop=0; iLoop<N; iLoop++)
{
//cout << "comparing " << token << " vs " << A[iLoop] << endl;
if (strcmp(A[iLoop],token)==0)
{
iReturn = iLoop;
break;
}
}
return(iReturn);
}
char * UpperCase ( char * token)
{
int n = strlen(token);
for (int iLoop=0; iLoop<n; iLoop++)
{
char ch = token[iLoop];
token[iLoop]= toupper(ch);
}
return(token);
}
int Go()
{
char A[20][MAX_WORD_LEN];
int freq[20];
char token[MAX_WORD_LEN];
int N;
int wordCount = 0;
for (int iLoop=0; iLoop<20; iLoop++)
{
freq[iLoop]=0;
memset(A[iLoop],0,MAX_WORD_LEN);
}
cout << " :> ";
cin >> N;
if (N>20)
{
N=20;
}
for (int iLoop=0; iLoop<N; iLoop++)
{
cin >> token;
//cout << "token is >" << token << "<" << endl;
char Token[MAX_WORD_LEN];
strcpy(Token,UpperCase(token));
//cout << "Token is >" << token << "<" << endl;
int iIndexPos = LinearSearch(Token, A,wordCount);
if (iIndexPos>=0)
{
//cout << " Token found at " << iIndexPos << endl;
freq[iIndexPos]++;
}
else
{
//cout << " storing token at " << wordCount<< endl;
strcpy(A[wordCount++],Token);
freq[wordCount-1]++;
}
}
cout << " WORD FREQ " << endl;
for (int iLoop=0; iLoop<wordCount; iLoop++)
{
cout << A[iLoop] << " " << freq[iLoop] << endl;
}
}
int main()
{
Go();
}