Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,999 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,386 people online right now. Registration is fast and FREE... Join Now!




Counting alphabetic characters, digits, punctuation, and space?

 
Reply to this topicStart new topic

Counting alphabetic characters, digits, punctuation, and space?, Counting!!!!!

colombia23
20 Jan, 2008 - 02:31 PM
Post #1

New D.I.C Head
*

Joined: 20 Jan, 2008
Posts: 3

___________________________Question?__________________________________________________
Write a program that will read a text file and count the number of alphabetic characters (isalpha), digits (isdigits), punctuation characters (ispunctuation), and whitespace (isspace) in the file.

I would appreciate it if anybody can suggest anything.
How would I finish the code I am stuck there are parts missing is there any help somebody can give me???????

The parts that I am not getting is how i can get the program to count the alphanumeric characters, digits, punctuation, and whitespace characters in the file. I don't know exactly what to use for the program to compile correctly. Also, in the else if statement what do I use to count the characters. I think I its similar but I don't know how to write correctly.
__________________________The Code___________________________________________
CODE

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define INPT "proj1.txt"

int main(void)
{
    int curCh;
    int preCh;
    int countCh = 0
    int isalpha = 0
    int isdigit = 0
    int ispunct = 0
    int isspace = 0
    FILE *fp;
    
{
//Statement
    if (!(fp = fopen(INPT, "r")))
    {
    printf( "Error opening proj1.txt for reading");
          return (1);
    } // if open error
    
    while ((curCh = fgetc(fp)) != EOF)
          {
          else if (curCh
              countisalpha ++;
          else if
               countisdigit++;
          else
               countispunct++;
          else
               countisspace++;
          preCh = curCh;
          }
          printf ("The number of alphanumeric characters is: %d\n", countalpha);
          printf ("The number of digits is: %d\n", countdigit);
          printf ("The number of punctuation characters is: %d\n", countpunct);
          printf ("The number of whitespaces is: %d\n", countspace);
          flose (fp);
          
  system("PAUSE");    
  return 0;
}


This post has been edited by colombia23: 20 Jan, 2008 - 04:10 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Counting Alphabetic Characters, Digits, Punctuation, And Space?
20 Jan, 2008 - 03:29 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,916



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Use code.gif tags when posting your code.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Counting Alphabetic Characters, Digits, Punctuation, And Space?
20 Jan, 2008 - 04:00 PM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Can you specify what portions are giving you difficulty?
User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: Counting Alphabetic Characters, Digits, Punctuation, And Space?
20 Jan, 2008 - 08:31 PM
Post #4

D.I.C Foot
Group Icon

Joined: 1 Aug, 2007
Posts: 983



Thanked: 2 times
Dream Kudos: 375
My Contributions
Here is the unfinished codes. You can add the if statements to check the numbers and space,
or edit it, as what is suited for you.

CODE
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string word;
    ifstream file;
    int countletters=0;
    int character = 0;
    int len = 0;
    int ascii;
    char letter;
    
    
    file.open("Myfile.txt"); //It must be in the same directory where your prog. is located

    if(!file)
    {
        cout << "Could not open file" << endl;
        return 1;
    }
    char ch;
    while(file.get(ch))
    {
            letter =  ch;
            ascii = letter;
            
            if((ascii>64)&&(ascii<91) || (ascii>96)&&(ascii<123)) //check countletters.
            countletters++;
            
            else if((ascii>=39)&&(ascii<=64))// check characters
            character++;
            
    }
    cout << "countletters are " << countletters << endl;
    cout << "characters are " << character << endl;
    
    file.close();
    
    
    return 0;
}



Edit: This is C++

This post has been edited by nirvanarupali: 20 Jan, 2008 - 10:03 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 12:14PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month