Welcome to Dream.In.Code
Become a C++ Expert!

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




Counting characters, digits, punctuation, etc.

 
Reply to this topicStart new topic

Counting characters, digits, punctuation, etc.

j14lu
20 Nov, 2007 - 05:15 PM
Post #1

New D.I.C Head
*

Joined: 20 Nov, 2007
Posts: 2


My Contributions
Here is what my teacher wants us to do for this lab

Write a program titled that will ask the user to enter four words of up to 20 characters each. The words may contain alphabetic, numeric, or punctuation characters, but will not contain spaces. For example, the user might enter “How”, “now”, “brown”, “cow!”. After the user has entered all four words, combine the words, with a space between each word, into a string that may contain up to 84 characters. After the words have been combined into a single string, the program should count how many of each of the following types of characters are contained in the string: upper case letters, lower case letters, consonants, vowels, digits, and punctuation marks. After the totals have been determined, the results should be displayed along with appropriate labels and the combined string. For example, in the sample entry above, the results could be displayed like this:

The string is: How now brown cow!

Upper case letters: 1

Lower case letters: 13

Consonants: 10

Vowels: 4

Digits: 0

Punctuation: 1

The program should give the user the option of entering additional sets of words until the user enters a signal to quit. The signal to be used is up to you, but if the program is not menu driven, the user should be told what the signal is to stop the program. The program must have at least 3 functions in addition to the main function.



here is what i have so far doesn't really work

CODE



#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstring>
using namespace std;

void ShowResults();

const int MAX = 21;

int main()
{
   int  index,
        countDigit,
        countUpper,
        countLower,
        countPunct;
   bool foundOther = false;
   char word1[MAX],
        word2[MAX],
        word3[MAX],
        word4[MAX],
        wordAll[85];
   countDigit = 0;
   countUpper = 0;
   countLower = 0;
   countPunct = 0;
  
   // Display message to tell the user what to do
   cout << "Please enter a word up to 20 charaters long with no spaces\n";
   cin  >> word1, MAX;
   cin  >> word2, MAX;
   cin  >> word3, MAX;
   cin  >> word4, MAX;
   strcat(wordAll, word1);
   strcat(wordAll, " ");
   strcat(wordAll, word2);
   strcat(wordAll, " ");
   strcat(wordAll, word3);
   strcat(wordAll, " ");
   strcat(wordAll, word4);
   cout << wordAll << '\n';
   //test input for non-alphanumeric characters
      for   (index = 0; index < (strlen(wordAll)); index++)
   {
         if (!isdigit(word1[index]))
         {
            countDigit++;
         }
   }
         for   (index = 0; index < (strlen(wordAll)); index++)
   {
         if (!islower(word1[index]))
         {
            countLower++;
         }
   }
         for   (index = 0; index < (strlen(wordAll)); index++)
   {
         if (!isupper(word1[index]))
         {
            countUpper++;
         }
   }
         for   (index = 0; index < (strlen(wordAll)); index++)
   {
         if (!ispunct(word1[index]))
         {
            countPunct++;
         }
   }
      
  
   cout << "Upper case letters:  "<<countUpper <<'\n';
   cout << "Lower case letters:  "<<countLower <<'\n';
   cout << "Digits:  " << countDigit <<'\n';
   cout << "Punctuation:   "<< countPunct <<'\n';
  
return 0;
}


User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Counting Characters, Digits, Punctuation, Etc.
20 Nov, 2007 - 09:03 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,166



Thanked: 78 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Here is what I would do.

I would read the string into an array, then run some tests on each unit in the array.

CODE

int Upper_Case=0,Digit=0;

for(i=0;i<sizeof(string);i++){
  if(isupper(string[i])) Upper_Case++;
  if(isdigit(string[i])) Digit++;
}


& test this on each of the conditions...


User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Counting Characters, Digits, Punctuation, Etc.
20 Nov, 2007 - 11:32 PM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 4,032



Thanked: 38 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Use these functions from ctype.h
  • isdigit
  • islower
  • ispunct
  • isspace
  • isupper

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 05:26AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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