GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Jun-2008, 12:38
VulturEMaN VulturEMaN is offline
New Member
 
Join Date: May 2008
Posts: 6
VulturEMaN is on a distinguished road

Recurrence of letters in a string


I've been working on this code for a while now, and for() loops always get me lost. I understand their concept, but geez they kill me.

The purpose of the program is to input a string that ends with a period, and to then calculate the number of words in the string and give an output of the reoccurance of the letters.

If I input:
I say Hi.

It should output:
3 words
1 a
1 h
2 i
1 s
1 y

the code is going in the right direction, but it messes up for some reason converting the string to lowercase, and the output tries to output all 26 letters, but seemingly skips a few...so i'm sorta confused what thats doing too. Help please?

CPP / C++ / C Code:
#include <iostream>
#include <cctype>
using namespace std;

void readAndCount (int &numWords, int letterCount[]);
// Reads a line of input. Counts the words and the number
// of occurrences of each letter.

void outputLetterCounts (int letterCount[]);
// Prints the number of occurrences of each letter that
// appears in the input line.

int main(){
  int numWords;
  int letterCount[26];  // stores the frequency of each letter

  cout << "Enter a line of text.." << endl << endl;

  readAndCount (numWords, letterCount);

  cout << endl;
  cout << numWords << " words" << endl;
  outputLetterCounts(letterCount);
  system("pause");
  return 0;
}

void readAndCount (int &numWords, int letterCount[]){
  int i = 0;
  unsigned int characterCount = 0;
  numWords = 0;
  string lineOfText;
  string madeLower(lineOfText);
  getline(cin,lineOfText);
  for (int i = 0; i < lineOfText.length( ); i++){
    madeLower[i] = tolower(lineOfText[i]);
    }
  while (madeLower[i] != '\0'){
    if (madeLower[i] == ' ' || madeLower[i] == ',' || madeLower[i] == '.')
      numWords++;
    else
      characterCount++;
    ++i;
    }
}

void outputLetterCounts(int letterCount[]){
  for (int i = 0; i < 26; i++){
    if (letterCount[i] > 0){
      cout << letterCount[i] << " " << char('a' + i) << endl;
	  }
  }
}
  #2  
Old 09-Jun-2008, 13:59
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: reoccurance of letters in a string....ugh


For starters, I don't see anywhere where you actually use the letterCount array. First, you need to initialize it so that all of the values start at zero. That is why you are getting weird values. Then, you'll have to increment the positions when you come across them.
  #3  
Old 09-Jun-2008, 18:18
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: reoccurance of letters in a string....ugh


Moreover, i didn't see that you actually counting each alphabet.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple questions for C++ project devster420 C++ Forum 1 20-Apr-2007 22:26
Message Class TransformedBG C++ Forum 5 29-Nov-2006 22:28
Need help with strings sasukekun C++ Forum 4 24-Apr-2006 11:51
variables return to previous value after i try to set them nasaiya MS Visual C++ / MFC Forum 2 14-Jun-2005 01:43
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 09:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:46.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.