Thread: Need some help with strings

  1. #1
    Registered User
    Join Date
    Nov 2006

    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

    #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;
      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
    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
    You have an extra one of these:
    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.

    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:

    My website:
    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, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08: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