Need some help with strings

This is a discussion on Need some help with strings within the C++ Programming forums, part of the General Programming Boards category; here is what I have to do Write a program that will read in a line of text and output ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Need some help with strings

    here is what I have to do

    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 upper-case and 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



    Here is what I have
    Can someone help me please
    I don't know what to do next



    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    void readAndCount (int &numWords, int letterCount[]);
    void outputLetterCounts (int letterCount[]);
    
    int main()
    {
      int numWords;
      int letterCount[26];
    
      cout << endl;
      cout << "Enter a line of text.." << endl << endl;
    
      readAndCount (numWords, letterCount);
    
      cout << endl;
      cout << numWords << " words" << endl;
      outputLetterCounts(letterCount);
     
      return 0;
    }
    
    void readAndCount (int &numWords, int letterCount[])
    
    
    
    void outputLetterCounts (int letterCount[])
    
    
    
    void outputLetterCounts(int letterCount[])
    {
      for (int i = 0; i < 26; i++)
        {
          if (letterCount[i] > 0)
         {
           cout << letterCount[i] << " " << char('a' + i) << endl;
         }
        }
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    What do you know how to do with cin? Do you know how to read in a single character? Do you know how to read in a whole line?

    Your outputLetterCounts looks ok for now, so work on readAndCount. How do you want to read in the user input?

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You have an extra one of these:
    Code:
    void outputLetterCounts (int letterCount[])
    I see you've included <cctype> . . . you'll probably need to use isspace() and/or isalpha() and/or toupper()/tolower(), which are in it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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