i have below code , now i want to calculate the frequency of each letter which a function contain an array of alphabets use Pointer to a function principle

i want to use pointer to find Frequency .... i have a little bit information about pointer but i don't know how to use it in this question

-----------------------


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main (){

char ch;
int c_tch=0,c_up=0,c_lo=0,c_sp=0,c_n=0,c_sym…


ifstream fileName;
fileName.open("a.txt");

while (fileName.get(ch)){

if (ch<=52)
c_tch++;

if(isupper(ch))
c_up++;

if(islower(ch))
c_lo++;

if(isspace(ch))
c_sp++;

if(isdigit(ch))
c_n++;

if(ispunct(ch))
c_sym++;

}
cout<<"Total Number Of Characters: "<<c_tch<<endl;
cout<<"Total Upper case: "<<c_up<<endl;
cout<<"Total Lower case: "<<c_lo<<endl;
cout<<"Total Space: "<<c_sp<<endl;
cout<<"Total Numeric: "<<c_n<<endl;
cout<<"Total Symbols: "<<c_sym<<endl;
fileName.close();
return 0;}

Recommended Answers

All 4 Replies

If you write a function and pass the data you read (like one big string) to it, you can use a pointer.

yes , i want to use pointer ,, can you help me

someone help me ......... :(

If you put your counts into a class, you could pass a pointer to the object (from the class) to be counted:

Here is a CRUDE example (you will need to build the class that contains the integer elements):

bool DoCounts(char* pstrFileName, CCharTypeCounts* pCounter, string& strError)
{
   char ch=0;
   bool blnRetVal = false;
   strError = "no data";
   ifstream fileIn;
   fileIn.open(pstrFileName);

   while (fileIn.get(ch))
   {
      blnRetVal=true;//crude
      if (ch<=52) pCounter->c_tch++;
      if(isupper(ch)) pCounter->c_up++;
      if(islower(ch)) pCounter->c_lo++;
      if(isspace(ch)) pCounter->c_sp++;
      if(isdigit(ch)) pCounter->c_n++;
      if(ispunct(ch)) pCounter->c_sym++;
   }

   fileIn.close();

   return blnRetVal;
}

Your main would call that function like this:

int main(void)
{
   CCharTypeCounts ctc;
   string strError="";

   if(!DoCounts("a.txt", &ctc, strError))
   {
      cout << "Could not process file: " << strError.c_str();
      return -1;
   }
/* You would need to build a REPORT method that returns the string of ALL of the counts
   cout << ctc.Report();
*/
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.