Jump to content

C++ Counting Help....????

- - - - -

  • Please log in to reply
1 reply to this topic

#1
jdjd_2009

jdjd_2009

    Newbie

  • Members
  • Pip
  • 2 posts
I need to count the amount of characters, whitespace, newlines, alphabetic, digits, and other.
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.


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I'm not sure why no one's said anything yet. Anyway, you're making this way harder than it has to be, all you really need is four separate functions and a simple data struct. The first one will take in an arbitrary char array value and run each char in that array (that does not equal \0) in your other three methods, an isAlpha(), isWhitespace(), and isDigit() functions. If all three of those return false, then it's other. The struct just contains the counted values, and since your method does not modify the contents of the char * the calling method should have it so no need to return that.

struct StringCount
{
    int alphas;
    int whitespace;
    int digits;
    int others;
};

bool isAlpha(char check)
{
    return (check >= 'a' && check <= 'z') || (check >= 'A' && check <= 'Z');
}

bool isWhitespace(char check)
{
    return check == ' ' || check == '\n' || check == '\t';
}

bool isDigit(char check)
{
    return check >= '0' && check <= '9';
}

struct StringCount countString(char *data)
{
    char check = 0;
    struct StringCount retVal;

    for (int i = 0; check = data[i]; ++i)
    {
        if (isAlpha(check)) retVal.alphas++;
        else if (isWhitespace(check)) retVal.whitespace++;
        else if (isDigit(check)) retVal.digits++;
        else retVal.others++;
    }

    return retVal;
}

Wow I changed my sig!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users