word analyzer

This is a discussion on word analyzer within the C++ Programming forums, part of the General Programming Boards category; Write a program that counts number of words in a sentence and displays the average number of letters in each ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    word analyzer

    Write a program that counts number of words in a sentence and displays the average number of letters in each word. Your program should ask the user to type a sentence (or sentences) that are 80 characters or less. If the sentence is longer than 80 characters, truncate it and notify the user of your action.
    Your program should call:
    • A function that accepts a string class object as its argument and returns the number of words contained in the string. For example, if the string argument is “Four score and seven years ago” the function should return the number 6.
    • A function that accepts a string class object as its argument and returns the number of punctuation characters (period, comma, exclamation, etc.) in the sentence(s).
    • A function that accepts a string class object as its argument and returns the average number of letters in each word.
    Your program should present the user with the following menu:
    1. Count number of words in the sentence
    2. Count number of punctuations in the sentence
    3. Compute the average number of letters in each word
    4. Enter another sentence
    5. Exit
    The program should exit when the user selects “E” to exit the program. Pass data to your functions by value, by reference, anduse static variables if necessary.


    Code:
    #include <iostream> 
    #include <iomanip> 
    #include <cctype> 
    #include <cstring> 
    using namespace std; 
    
    const int MAX = 80;
    int countWords(const string&);
    int countPunc(const string&);
    
    int main(int argc, char *argv[]) { 
    string input, choice;
    char ch;
    
    cout << "Enter a string: ";
    getline(cin,input);
    
    do {
    cout << " MENU" << endl;
    cout << "A> Count the Number of words in the sentence." << endl;
    cout << "B> Count the number of punctuations in the sentence." << endl;
    cout << "C> Compute average." << endl;
    cout << "D> Enter another string." << endl;
    cout << "E> Exit this program." << endl; 
    cout << "Enter A, B, C, D, OR E." << endl;
    getline(cin,choice);
    
    switch (ch = tolower(choice[0])) {
    case 'a': 
    cout << "The string has " << countWords(input) << " vowels." << endl;
    break;
    case 'b': 
    cout << "The string has " << countPunc(input) << " consonants." << endl;
    break;
    case 'c': 
    cout << "The average " << countAvg(input) << " yeah " << endl;
    break;
    case 'd': 
    cout

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    So where is the rest of the code?
    It just ends at line 39.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. Lexical Analyzer
    By ammad in forum C++ Programming
    Replies: 8
    Last Post: 11-18-2009, 05:59 PM
  3. Lexical analyzer for C
    By nishkarsh in forum C Programming
    Replies: 4
    Last Post: 08-26-2008, 08:05 AM
  4. packet analyzer in c
    By althagafi in forum Networking/Device Communication
    Replies: 16
    Last Post: 08-06-2004, 03:35 PM
  5. stat analyzer available on the net?
    By iain in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-21-2002, 07:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21