3 Replies - 11726 Views - Last Post: 20 January 2008 - 09:31 PM Rate Topic: -----

#1 colombia23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-January 08

Counting alphabetic characters, digits, punctuation, and space?

Post icon  Posted 20 January 2008 - 03:31 PM

___________________________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___________________________________________
#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 January 2008 - 05:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Counting alphabetic characters, digits, punctuation, and space?

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Counting alphabetic characters, digits, punctuation, and space?

Posted 20 January 2008 - 04:29 PM

Use :code: tags when posting your code.
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Counting alphabetic characters, digits, punctuation, and space?

Posted 20 January 2008 - 05:00 PM

Can you specify what portions are giving you difficulty?
Was This Post Helpful? 0
  • +
  • -

#4 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,119
  • Joined: 01-August 07

Re: Counting alphabetic characters, digits, punctuation, and space?

Posted 20 January 2008 - 09:31 PM

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.

#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 January 2008 - 11:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1