I've been at this for hours and have had no breakthrough. I would appreciate it if someone could help please.
Description:
Write a C++ program that tabulates the occurrence of characters in the input, grouped according to whether whitespace, alphabetic, digits, or other.
Input format:
Arbitrary ascii characters
Output format:
c characters in input:
w whitespace (w.w%)
a alphabetic (a.a%)
d digits (d.d%)
o other (o.o%)
Input examples:
Example 1) "10 Boeing 747's will be scrapped.\n"
Example 2) ""
Output examples:
Example 1)
34 characters in input
6 whitespace (17.6%)
1 newlines (2.9%)
21 alphabetic (61.8%)
5 digits (14.7%)
2 other (5.9%)
Example 2)
0 characters in input
Here is the code I have right now------>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
char previousCH, ch ,nextchar ;
int characters = 0 , //total number of whitespace, alphbetic, digits, other
whitespace = 0 , // number of spaces
newlines = 0 , // number of new lines
alphabetic = 0 , // number of letters
digits = 0 , // number of digits
other = 0 , // number of others
bool charcheck (char c)
{
if ((c >='a' && c <= 'z') || ((c >= 'A') && (c < 'Z')))
return 1;
else
return 0;
}
cout << "Please enter input with '$' character:\n\n";
while(nextchar != '$' ) // "$" is the end of file or input
{
nextchar = cin.peek();
if (nextchar == '\n') // ignore or eat new line character
cin.ignore(1,'\n');
previousCH = ch; // this character will be previous one the next loop
cin.get(ch);
if (charcheck(ch))
{
alphabetic++;
longtemp++;
}
else
{
other++;
//.... don't count a word if you found a space unless it was preceeded or followed with characters
if ((ch== ',') || (ch == '.') || (ch == ' ' && (charcheck(cin.peek())) && (charcheck(previousCH))))
{
longtemp = 0;
if (ch == ' ') whitespace++;
}
if (ch == '.')
}
}
other--; // ignore '$' character
cout << "______________________________________________________________\n";
cout << "\nThe number of characters:\t" << (whitespace+alphabetic+digits+other) << endl;
cout << "\nThe number of white space:\t" << whitespace << endl;
cout << "\nThe number of new lines:\t" << newlines << endl;
cout << "\nThe number of alphabetic letters:\t" << alphabetic << endl;
cout << "\nThe number of digits:\t" << digits << endl;
cout << "\nThe number of others:\t" << other<< endl;
getch();
}
Edited by dargueta, 19 October 2010 - 08:20 PM.
Please use code tags next time.


Sign In
Create Account

Back to top









