Hey all,
Erm given the problem
Write a program that will read in a line of text and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, and periods. When outputting the number of letters that occur in a line, be sure to count upperand lowercase versions of a letter as the same letter. Output the letters in alphabetical order and list only those letters that do occur in the input line. For example, the input line
I say Hi.
should produce output similar to the following:
3 words
1 a
1 h
2 i
1 s
1 y
I've tried to write the program source code for this one but I've been alot of errors which I cant fix. If any of you can tell what's wrong with my program and fix it for me I'd really appreciate it. My source code thusfar is as follows:
#include <iostream.h>
#include <stdio.h>
#include <ctype.h>
using namespace std;
int main()
{
int i,h[80]={0},words=1;
char s[15];
cout<<"Enter the String: ";
gets(s);
for(i=0;s!='\0';i++)
{
if(s==' ')
words++;
h[tolower(s)-'a']++;
}
cout<<"\n"<<words<<" words";
for(i=0;i<26;i++)
if(h!=0)
cout<<"\n"<<h<<" "<<(char)(i+97);
system ("pause");
return 0;
}
/*
limitaitons
- will give wrong output for a statement with more than one space between words
- wrong output for leading and trailing spaces
*/
I seriously cannot understand what's wrong with this code. If you guys can fix it it'd be really nice. Also, please please please give me comments along the way, otherwise I'll get lost halfway through.