Hello there,
Okay well i have tried to do this program but i'm just not sure if i'm going in the right direction. Well i have to create a text file (Did that) which is inputed into the program, and the program has to count the instances of the following categories of symbols. such as Uppercase and lower case letters, digits, End of sentence markers like periods, question marks, and explanation points, also commas, semicolons, and colons, and Blanks. Okay well i have written some code and i used a switch statement in my processing. but the thing is, i'm not sure if i'm doing it right. And after all these satistics, i have to use them to approximate the average word and sentence length.I need another's help on this if you would, i'd be greatful for your help or for anything. Well happy holidays and thanks for your time.

Dani AI

Generated

Building on the OP's requirements (posted by ) and the per-character ideas from , and , a practical workflow is: read the file (streamed for very large files), update counters for the exact characters you care about in one pass, then run a separate, regex-based pass to find words and sentences and compute averages. Define terms up front: treat "blanks" as ASCII space (or optionally all whitespace), count sentence terminators as . ? !, average word length as alphabetic characters per matched word, and average sentence length as words per sentence (this is approximate — abbreviations and decimals will skew counts).

import re
from collections import Counter

def analyze_file(path, treat_whitespace_as_blanks=False):
    counts = Counter({
        'upper':0, 'lower':0, 'digits':0,
        'periods':0, 'questions':0, 'exclaims':0,
        'commas':0, 'semicolons':0, 'colons':0,
        'spaces':0
    })
    with open(path, 'r', encoding='utf-8', errors='replace') as f:
        text = f.read()

    for ch in text:
        if ch.isupper(): counts['upper'] += 1
        elif ch.islower(): counts['lower'] += 1
        if ch.isdigit(): counts['digits'] += 1
        if ch == '.': counts['periods'] += 1
        elif ch == '?': counts['questions'] += 1
        elif ch == '!': counts['exclaims'] += 1
        elif ch == ',': counts['commas'] += 1
        elif ch == ';': counts['semicolons'] += 1
        elif ch == ':': counts['colons'] += 1
        if ch == ' ': counts['spaces'] += 1
        elif treat_whitespace_as_blanks and ch.isspace(): counts['spaces'] += 1

    words = re.findall(r"[A-Za-z0-9]+(?:['-][A-Za-z0-9]+)*", text)
    num_words = len(words)
    total_letters = sum(len(re.sub(r'[^A-Za-z]', '', w)) for w in words)
    avg_word_len = (total_letters / num_words) if num_words else 0

    sentences = [s.strip() for s in re.split(r'(?<=[.!?])\s+', text.strip()) if s.strip()]
    num_sentences = len(sentences)
    avg_sentence_words = (num_words / num_sentences) if num_sentences else 0

    return counts, num_words, num_sentences, avg_word_len, avg_sentence_words

Notes and common gotchas: simple splitting treats "Mr.", "e.g." and numeric decimals as sentence ends, and ellipses ("...") will inflate terminator counts — acceptable for classroom work but not for linguistically precise results. For very large files, process in chunks and update counters incrementally instead of loading the whole file. For production-grade sentence boundaries, prefer a trained sentence tokenizer (for example the Punkt models in NLP toolkits). Always guard against division by zero when computing averages.

Recommended Answers

All 7 Replies

try this

make a seperate counter for each instance
read the file 1 character at a time
for the upperccase and lowercase you can use the functions isupper and islower and whenever the return is 1 incriment the respective counters as for the other instances, use the ascii values of the characters

You can do somthing like this:

#include <iostream>
using namespace std;
int main()
{
    char test[] = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.!:;? ";// all the chars you are testing for
    int size = sizeof(test)/sizeof (char); // size of test array
    int counter[size]; // make array of int's
    string s1;
    cout << "enter stringn";
    cin >> s1; // get string

    for(int i = 0; size > i; i++)
    { // rest all of counter to 0
        counter[i] = 0;
    }

    for (int i = 0; s1[i] != '\0'; i++) // loop theu the s1 string
    {
        for(int j = 0; size > j; j++)
        {
            if (s1[i] == test[j]) // check if it match
            {
                counter[j]++; // if, the add one to the right counter
            }
        }
    }

    for(int i = 0; size > i; i++)
    { // cout all the letters, and the frequency
        cout << test[i] << " : " << counter[i] << 'n';
    }
}

just need to change it to get the input from a fil instead of screan :)

Nice. :-)

well... all symbols are assigned from 0 to 255... why not create an array of counter which count the number of ascii value which is the same as it index.. and in the same time the index is the ascii value of the symbol counted.... my algo for that ...^___^ hehe...

#include<string.h>
#include<stdio.h>
main()
{
char buffer[1000] = "series of text input from file...";
/* assuming u know how to get the content of the file and then store it into a buffer data (example buffer[1000] ) 100 characters */

int counter[256]; /* counter for each ascii or symbol */

for(x=0;x<255;x++){
counter[x]=0; /* initialize all to zero..*/
}

for(y=0;y<strlen(string);y++){
ascii_value = buffer[y];
counter[ascii_value] ++;
}

/*displaying the result..
for(z=0;z<255;z++){
printf("%c = %d ",z, counter[z]);
}
getch();
}

any clarification <<email snipped>>
those code are in C anyways.. c and c++ are much
alike.. so just code it to c++ base on my posted code if
u have understood it...
;)

ivanCeras plz use code tags.

Let sleeping threads lie.

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.