Question:
How do I count alpha and numeric characters in a text file? C++?
2014-02-09 14:17:15 UTC
I know I'll have to use isalpha and isdigit functions but I have no idea how to employ those along with reading the text file. Would I have to read each individual character in? And how would I do that? Also how could I ask the user to input a single character and count how many times that character appears in the text file?
Three answers:
MichaelInScarborough
2014-02-11 20:24:32 UTC
This is my approach:

#include

#include

#include

using namespace std;



void count(const string& strFile, int& alpha, int& digits)

{

ifstream is(strFile.c_str());

char c; // Declare a character

if (is.good()) {

while(!is.eof()) {

is >> c; // read in one character at a time

if (isdigit(c)) {

++digits;

}

else if (isalpha(c)) {

++alpha;

}

}

}

}

int main()

{

int a = 0, d = 0;

count("football.txt", a, d);

cout << "football.txt contains " << a << " alpha character and " << d << " digits!" << endl;

return 0;

}
2014-02-09 22:27:32 UTC
A general information:



1. take some indicators, int al=0, int nu=0, char ch="character by user",int count=0.

2. read each character until any space, comma or full stop is encountered.

3. if ,in between that, characters lie between alphabetic range of ascii table , increase al by one (al++),

4. if characters lie between numeric range of ascii table, increase nu by one (nu++),

5. and keep comparing each character with "ch" and if it matches , increase the count by one (count++).





This is a very good exercise.
cja
2014-02-10 12:52:16 UTC
A map is a great choice for this problem. If you're not familiar with the map container, here's an opportunity for you to learn about it. You can do something like this:



#include

#include

#include

#include

#include



using namespace std;



int main(int argc, char *argv[]) {

    map chMap;

    string line, filename;

    string::iterator j;



    cout << "Enter filename: ";

    getline(cin, filename);

    ifstream infile(filename.c_str());

    if (infile.is_open()) {

        while (getline(infile, line)) {

            for (j = line.begin(); j != line.end(); j++) {

                if (isalpha(*j) || isdigit(*j)) {

                    if (chMap.find(*j) == chMap.end()) {

                        chMap.insert(pair(*j, 1));

                    } else {

                        ++chMap[*j];

                    }

                }

            }

        }

        infile.close();

        while (true) {

            cout << endl << "Enter an alpha/numeric character: ";

            getline(cin, line);

            if ((line.size() > 0) && isalpha(line[0]) || isdigit(line[0])) {

                cout << "count of " << line[0] << " in "

                          << filename << " = " << chMap[ line[0] ] << endl;

            }

        }

    } else {

        cout << "error opening " << filename << endl;

    }

    return 0;

}



#if 0



Sample run:



Enter filename: tb.txt



Enter an alpha/numeric character: a

count of a in tb.txt = 15



Enter an alpha/numeric character: 1

count of 1 in tb.txt = 5







Where:



$ cat tb.txt

Forward LaMarcus Aldridge scored 26 points and guard

C.J. McCollum came off the bench to score 19 to lead

the Portland Trail Blazers to a tougher-than-expected

117-110 victory over the injury-depleted Minnesota

Timberwolves on Saturday night.



#endif


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...