?
Solved

Number of occurence of a letter, word, etc in a string.

Posted on 2000-04-28
9
Medium Priority
?
275 Views
Last Modified: 2011-09-20
Can someone help me with this program.  It is not working right.  I am suppose to write a program that reads in a line of text and outputs the number of words, vowels, constants in the line and the number of occurrence of each letter (The other part I don't know how to do)?  Here is what I got so far.

#include<stdlib.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#define TEXT_FILE "a:\\text112.dat" //defines the file to use for data
#define VOWELS "aeiouAEIOU" //define what a vowel is


void display_header(void);
void countVowelsConsonantsSpaces( char *line );//FUNCTION PROTOTYPE
// *line is POINTER TO ARRAY it points to the first element in
//  text array

  int main(void)
  {
     // VARIABLE DECLARATION
const unsigned int max_lines=25;
const unsigned int line_length=75;
char text[max_lines][line_length+1]; //array to store max_lines and line_length
int  line_counter,num_lines,total; //variables
int  vowels_per_line,count_vowels;
int  spaces_per_line,total_spaces;
int  consonants_per_line,total_consonant;





 // OPEN THE FILE
  ifstream text_file( TEXT_FILE ); //declares an input file object,
             //data is extracted from this file

  // IF UNABLE TO OPEN FILE EXIT THE PROGRAM
  if( !text_file )
   {
  cout<<"CANNOT FIND THE "<<text_file<<"FILE";
  cout<<"CHECK TO SEE IF IT IS ON A: DRIVE"<<endl;
  exit(1);
   }

   num_lines=0;
   display_header();// function call to display my heading

   while( !text_file.eof() )   // NOT END OF FILE CONTINUE TO READ DATA IN
   {
   //getline function to extract data from file
  if( !text_file.getline(text[num_lines], line_length ) )
   continue;

  // Intialize counter. Set each line count to zero
  vowels_per_line  = 0;
  spaces_per_line  = 0;
  consonants_per_line = 0;

  countVowelsConsonantsSpaces( text[num_lines] );//function call

  cout <<  " \t\tVol= " << vowels_per_line     << "\tCon= "
        << consonants_per_line << "\tSpaces= " << spaces_per_line << "";


  num_lines++;

  // Update totals
  count_vowels  += vowels_per_line;
  total_consonant += consonants_per_line;
  total_spaces  += spaces_per_line;
 }


 text_file.close();

 // No lines were read

 if( num_lines == 0 )
  exit(1);

 cout << "TOTAL LINES : " << num_lines << " VOWELS: " << count_vowels << 
       " CONSONANTS: " << total_consonant << " SPACES: " << total_spaces << " ";

return 0;
  }// END OF MAIN

/******************************************************************************/
  void  display_header(void)
 {
 cout<<"COUNTING IS FUN";
 cout<<"This program will count the number of";
 cout<<"vowels,consonants,and spaces";
 }
/******************************************************************************/
void countVowelsConsonantsSpaces( char *line )
{
  int azCount = 0;

  int  vowels_per_line,count_vowels;
  int  spaces_per_line,total_spaces;
  int  consonants_per_line,total_consonant;

// Check for vowel
 for( int i=0; i < strlen( line ); ++i ) //for loop to count #of letters
 if( line[i] >= 'A' && line[i] <= 'z' )// in the lines
  { ++azCount;  //add up all letters

   for( int ii=0; ii < strlen( VOWELS ); ++ii )//for loop to count #vowels
  if( line[i] == VOWELS[ii] ) //text array =vowels in line
   ++vowels_per_line;  //add up the vowels.
  }
  else
   if( line[i] == ' ' ) // array= spaces
   ++spaces_per_line;   //add up spaces

   consonants_per_line = azCount - vowels_per_line;//sub. vowels
}                                                      //remainer is consonants


// End of countVowelsConsonantsSpaces
0
Comment
Question by:madhour
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2761621
Next is you programm(with file begtest.cpp"

#include<stdlib.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<ctype.h>
 
#define TEXT_FILE "begtest.cpp" //defines the file to use for data
#define VOWELS "aeiouAEIOU" //define what a vowel is


void display_header(void);
void countVowelsConsonantsSpaces( char *line,int& i1,int &i2,int &i3 );//FUNCTION PROTOTYPE
// *line is POINTER TO ARRAY it points to the first element in
//  text array

int main(void)
  {
     // VARIABLE DECLARATION
const unsigned int line_length=75;
char text[line_length+1]; //array to store line
int  num_lines; //variables
int  vowels_per_line = 0,count_vowels = 0;
int  spaces_per_line = 0,total_spaces = 0;
int  consonants_per_line = 0,total_consonant = 0;

 // OPEN THE FILE
  ifstream text_file( TEXT_FILE ); //declares an input file object,
             //data is extracted from this file

  // IF UNABLE TO OPEN FILE EXIT THE PROGRAM
  if( !text_file )
   {
  cout<<"CANNOT FIND THE "<<text_file<<"FILE" << endl;
  cout<<"CHECK TO SEE IF IT IS ON A: DRIVE"<<endl;
  exit(1);
   }

   num_lines=0;
   display_header();// function call to display my heading

   while( !text_file.eof() )   // NOT END OF FILE CONTINUE TO READ DATA IN
   {
   //getline function to extract data from file
    if( !text_file.getline(text, line_length ) )
   continue;

  // Intialize counter. Set each line count to zero
  vowels_per_line  = 0;
  spaces_per_line  = 0;
  consonants_per_line = 0;

  countVowelsConsonantsSpaces( text,
      vowels_per_line,consonants_per_line,spaces_per_line);//function call

  cout <<  " \t\tVol= " << vowels_per_line     << "\tCon= "
  << consonants_per_line << "\tSpaces= " << spaces_per_line << "" << endl;


  num_lines++;

  // Update totals
  count_vowels  += vowels_per_line;
  total_consonant += consonants_per_line;
  total_spaces  += spaces_per_line;
 }


 text_file.close();

 // No lines were read

 if( num_lines == 0 )
  exit(1);

 cout << endl << "TOTAL LINES : " << num_lines << " VOWELS: " << count_vowels << 
" CONSONANTS: " << total_consonant << " SPACES: " << total_spaces << " ";

  return 0;
}// END OF MAIN

/******************************************************************************/
  void  display_header(void)
 {
 cout<<"COUNTING IS FUN";
 cout<<"This program will count the number of";
 cout<<"vowels,consonants,and spaces";
 }
/******************************************************************************/
void countVowelsConsonantsSpaces( char *line,
      int &vowels_per_line,int &consonants_per_line,int &spaces_per_line)
{
  int azCount = 0;

// Check for vowel
 for( int i=0; i < strlen( line ); ++i ) //for loop to count #of letters
   {
     if ( isalpha(line[i]))// in the lines
       {
         ++azCount;  //add up all letters
         if (strchr(VOWELS,line[i])) //text array =vowels in line
           ++vowels_per_line;  //add up the vowels.
       }

     else if( line[i] == ' ' ) // array= spaces
       ++spaces_per_line;   //add up spaces
   }
 consonants_per_line = azCount - vowels_per_line;//sub. vowels
}                                                      //remainer is consonants
I hope, it helps. Alex

0
 

Author Comment

by:madhour
ID: 2762379
Adjusted points from 100 to 150
0
 

Author Comment

by:madhour
ID: 2762380
Alex, I do not understand what you did.  The does not display anything now.  And why did you make the incoming text file with a .cpp extension.  Thanks for your help though.  I'm increasing the points to 150.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2762429
It is very simple. For test code
I use file begtest.cpp
You change back it to you file
#define TEXT_FILE "a:\\text112.dat"
instead of
#define TEXT_FILE "begtest.cpp"
0
 

Author Comment

by:madhour
ID: 2762490
I did that already and the code still does not work.  It does not even show the counters for some reason.  I will have to look at it later.  Thanks though.
0
 
LVL 1

Expert Comment

by:ZivKatzir
ID: 2763483
you will have to forgive me for interuppting the disscusion between you and Alex. Though it seems to me that there is a simple solution to your problem.
All letters are arranged in the ascii table (low letters first and then capitals). If all what you want to do is to count the acourences of each different letter just crate an array of counters (one for every letter) now (as you currently do) read the string charechter at a time. All id left to do is to increase the prorper counter.

Symple tip since the letters are arranged in sequance you can refer the right counter in the array with no IF or SWITCH sentance.

If you need me to I can write you a complete answer.
0
 
LVL 1

Expert Comment

by:tomer_engel
ID: 2763604
ZivKatzir gave me a nice idea(more memory efficiant)--> "load" a line and "arrange" then ascii values--sort them by value,, and then you'll have the numbers of occurences.
 (thogh ziv's answer is more  easy to implement and debbug)        
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 600 total points
ID: 2763855
What exectly don't work? I have tested
code before sending and all works.
Test again.
Alex
0
 

Author Comment

by:madhour
ID: 2781337
Thanks, the program does work great.  Sorry for taking so long.  I had to get another project out of the way first before I finish this one.  
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question