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.

OK, since you are new, allow me to correct things in your program first before tackling the coding of the problem. By the way, what's an erm? ;)

Problem#1: Format your code! You need to learn to indent your code properly so it can be followed, and it's better to learn it now than unlearn a bad habit later. This includes spacing: for(i=0;s!='\0';i++) vs for (i = 0; s != '\0'; i++) Problem#2: gets() -- just stop using it. Period. Forget it exists!

Problem#3: system ("pause"); -- see #2

limitaitons
- will give wrong output for a statement with more than one space between words
- wrong output for leading and trailing spaces

That's because all you are doing is counting spaces, not words. You need to decide what represents a word. Each time you see the beginning of a word, add one to the word count and set a flag that indicates you are in a word. Each time you see the ending of a word, set that same flag to indicate you are not in a word. A simple flag = 0; and flag = 1; (false=0 and true=1) would work fine for the flag. Now you have to figure out what constitutes the beginning and end of a word.

Since you already understand that characters are just values (very good for a new programmer!) you can simply set up your h array to be 127 instead of 80 and count every character that you see. It takes less code in your initial loop. Still use the tolower() function because it only works on letters. Then you have a count of all characters.

In your 2nd loop isn't h an array? If so, what does if (h != 0) do would you suppose?

And give h a better name. What is it's purpose? Create a name that indicates said purpose.


And your two posting problems:

I seriously cannot understand what's wrong with this code. If you guys can fix it it'd be really nice.

Yes it would, but it's not going to happen. We'll help you fix it though

Also, please please please give me comments along the way, otherwise I'll get lost halfway through.

Then you should have commented the code yourself... You can't ask us to do more than you are willing to do for yourself. ;)

Please read the the Rules

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.