Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,004 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,382 people online right now. Registration is fast and FREE... Join Now!




program to find that counts how many words, count the longest word, di

 
Reply to this topicStart new topic

program to find that counts how many words, count the longest word, di, C++

Rating  4
kryptonite
13 Jan, 2008 - 07:14 AM
Post #1

New D.I.C Head
*

Joined: 13 Jan, 2008
Posts: 2

so far i have created a program that allows the user to type a sentence, the program counts how may words, digits and counts the longest word but i am finding it difficult to count each letters and display them pls can anyone help me with the code !!!!!!!!!!! blink.gif

The code:
CODE

#include <iostream>
using namespace std;
#include <conio.h>
#include <iomanip>
# include <string>

const int SIZE = 100;
void input (char*);
void wordCount (char*);
void longWord (char*, int);
void numbers (char*);
void outputLetterCounts (int letterCount[]);

int main()
{
char string [100] = {'/0'};
char word[100] = {'/0'};
int x = 0; int n = 0;
int letterCount[100] = {0};


input(string);
wordCount(string);
longWord(string, x);
numbers (string);
outputLetterCounts(letterCount);
return 0;
}

void input (char *enter)
{
cout<<"Enter sentence(s) "<< std::endl;
std::cin.getline(enter, SIZE);
int len = strlen( enter );

    }
void wordCount (char *word2)
{
int cnt = 0;

while(*word2 != '\0')
{
while(isspace(*word2))
{
++word2;
}
if(*word2 != '\0')
{
++cnt;
while(!isspace(*word2) && *word2 != '\0')
++word2;
}
}
std::cout<<"Number of words: "<<cnt<<endl;

}
void outputLetterCounts(int letterCount[])
{
  for (int l = 0; l < 26; l++)
    {
      if (letterCount[l] > 0)
    {
      cout << letterCount[l] << " " << char('a' + l) << endl;
    }
  }
}

void numbers (char *word2)
{

int num = 0;
int amount = strlen(word2);

for(int i = 0; i < amount; i++)
{
if(isdigit(word2[i]))
num++;

}
std::cout<<"Digits: "<<num<<endl;

}
void longWord (char *temp, int x )
{
  int counter = 0;
  int max_word = -1;
  
  int length = int(strlen(temp));
  
  for(int i=0; i<length; i++)
  {
      if(temp[i] !=' ')
      {
          counter++;
      }
      else if(temp[i]==' ')
      {
          if(counter > max_word)
          {
              max_word = counter;
          }
          counter = 0;
      }
                
  }  
  std::cout <<"Longest word:" << max_word;  

}

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Program To Find That Counts How Many Words, Count The Longest Word, Di
13 Jan, 2008 - 07:21 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,983



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
Don't forget to code.gif
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Program To Find That Counts How Many Words, Count The Longest Word, Di
13 Jan, 2008 - 07:29 AM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
I suggest you use the function isalpha(/*some char here*/), but include the cctype.h first.


User is offlineProfile CardPM
+Quote Post

TheMagnitude
RE: Program To Find That Counts How Many Words, Count The Longest Word, Di
13 Jan, 2008 - 07:33 AM
Post #4

D.I.C Head
Group Icon

Joined: 12 Jan, 2008
Posts: 88


Dream Kudos: 125
My Contributions
Please don't use l as a variable it can be easily confused with 1 eg:
CODE
var=1-l+l-l+1


And what do you mean count how many letters? count how many letters for each word, or count total letters?
User is offlineProfile CardPM
+Quote Post

kryptonite
RE: Program To Find That Counts How Many Words, Count The Longest Word, Di
13 Jan, 2008 - 01:56 PM
Post #5

New D.I.C Head
*

Joined: 13 Jan, 2008
Posts: 2

sorry for my for my bad explaination i should have included an example of expected output

example of the result:

hello worlds 1

wordcount: 11
longest word: 6
digits: 1
letter count:1 h
1 e
3 l
2 o
1 w
1 r
1 d
1 s
so far i can display all expect letter count. pls any sugestion

User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Program To Find That Counts How Many Words, Count The Longest Word, Di
14 Jan, 2008 - 01:12 AM
Post #6

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

Well, you can use, as an earlier poster suggested, the "isalpha" function. Also there is an "isdigit" function. Finally there is a "tolower" function. All three are in the "cctype" library. I would have your lettercount array be an array of 26 integers rather than 100 since there are 26 letters in the alphabet. It looks like you are already using "isdigit" and have the "digit" section under control. For the letter tally section, assuming that the user entry is in a character array called "string" as you have it (I would change the name to something else to avoid confusion with the string class), you can do this (assuming lettercount is initialized to all zeroes already, as it is):

1. Go through "string" character by character starting with character 0..
2. Using "isalpha" function, check if it is a letter.
3 If it isn't, go to step 7.
4. Using "tolower" function, convert letter to lower case.
5. Subtract 97 (which is the ASCII offset of 'a') from the resulting character to get the appropriate index of lettercount.
6. Increment lettercount for the index from step 5.
7. Go to next character in "string".
8. If more letters are left, go to step 2.

You'll end up with lettercount[0] containing the number of a's, lettercount[1] containing the number of b's, lkettercount[2] containing the number of c's, etc.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 12:33PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month