Solved

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

Posted on 2000-04-28
9
239 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 150 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 411
How to copy an image file into clipboard C/C++? 1 151
Better understanding on C++ Class serialization and formats 9 59
C++ question 3 61
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now