i need to write a program that can count the number words the user enters and and the recurences of each letter.
ex: Have a nice day
Total words: 4
a = 3
d = 1
e = 2
...
Can't get the program i've writen so far to count the number of words.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
char letters[] = "abcdefghijklmnopqrstuvwxyz";
int freq[sizeof letters - 1] = {0};
char text[60];
char input;
int numWords=0;
bool inword = false;
cout<<"Enter a line of text: \n";
cin>>input;
while((input=cin.get()) !='\n')
{ // Counts the number of words in the text.
if(input == ' ' || input== '\t')
{
inword = false;
}
else if (inword == false)
{
inword = true;
numWords++;
}
cout<<"The number of words in the text is: "<<numWords<<endl;
if ( cin.getline ( text, sizeof text ) )
{
for ( int i = 0; text[i] != '\0'; i++ )
{// Converts all letters to lower case.
if ( isalpha ( text[i] ) )
++freq[tolower ( text[i] ) - 'a'];
}
for ( int i = 0; letters[i] != '\0'; i++ )
{// Counts the frequency of all letters.
if ( freq[i] != 0 )
cout<< letters[i] <<": "<< freq[i] <<endl;
}
}
}
return 0;
}
thanks...