Analyzing a text file.

I have to a write a program for a class that analyzes a text file. It is supposed to count the total number of characters, alpha character (a-z), numerals (0-9), single specified characters (user input needed to search for the character), and the number of lines within the file. I got the basics for how to stream in the file but I don't know how to actually analyze the information I need. How do I go about doing this?

not sure by what you meant so ill just type what i understand:

number of lines within the file:
try using the getline function and use a counter. getline gets the whole line instead of taking one word (notice how cin only takes the first word). then, just make a variable (ex. ctr = 0 ) to count the number of times the getline function is used.

total number of characters:
are punctuation marks included here? if not make a loop (use either "for" or "while" loop ) that has a variable initialized to zero (int ctr = 0) and will continue to increment until it is equal to the length of the string. since your on fstream now i assume you know about arrays and strings right? use the variable (ctr) to access every character of the string and use isalnum to check if it is a number or an alphabet.

not sure if i explained it correctly so ill just write an example below:
1
2
3
4
5
6
7
8
9
10
    int ctr = 0;
    string word;
    getline(cin, word);      ///cin inside the parenthesis can be replaced by the varaible you used in ifstream
   /// ex. if your code has ifstream getIt("name.txt"); you type getline(getIt, word);
   for(int x = 0; x != word.length(); x++){
        if(isspace(word[x])){
            ctr++;      /// if the character is a space, ctr = ctr + 1
        }
    cout<<"this line has "<<word.length() - ctr<<" charaters.";  
   }



looking for alphabets and number is practically the same except you use isalpha and isdigit and if the user is looking for a certain character then you can just store the letter in a temporary variable and make a loop to check .

now try making your own program. good luck ^^

REFERENCE:
getline: http://www.cplusplus.com/reference/string/string/getline/?kw=getline
length(): http://www.cplusplus.com/reference/string/string/length/

isalnum, isalpha, and isdigit are all in the ctype.h header file:
http://www.cplusplus.com/reference/cctype/
Last edited on
Can you tell me if I'm starting to have the right idea? I found a way to count all the characters? But I'm sure I have the isalpha function wrong.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;

// Declare constants

// Function Prototypes

int main()
{
// Declare variables below here
string fileName, line;
char ch, x;
int charCount, alphaCount, numCount, spefCount, lineCount;
ifstream infile;
ofstream outfile;
// Initialization Section for real number output. DO NOT MOVE!
cout <<setiosflags(ios::fixed | ios::showpoint);
cout <<setprecision(2);
// Begin your "main processing" below here
charCount=0;
alphaCount=0;
numCount=0;
spefCount=0;
lineCount=0;
cout<<"Please enter the file name >> ";
cin>>fileName;

infile.open(fileName);

cout<<"Please enter a single specific character to search for: "<<endl;
cin>>x;


if (infile.is_open())
{

while( getline (infile, line))
{

charCount += line.length();;

}
}



while (infile.get (ch))
{
if (isalpha(ch))
{

alphaCount++;
}
}






cout<<"Number of characters: "<<charCount<<endl;
cout<<"Number of alpha characters (a-z): "<<alphaCount<<endl;
cout<<"Number of numeric characters (0-9): "<<numCount<<endl;
cout<<"Number of user specified characters: "<<spefCount<<endl;
cout<<"Number of lines: "<<lineCount<<endl;

infile.close( );

return 0;
}

// function definitions below here
Topic archived. No new replies allowed.