-1

I've written a program to count no. of uppercase, lowercase, spaces and special characters. I want to make separate functions to count each type. How can I do that?

#include<iostream>

using namespace std;

int main()
{
   int upper = 0, lower = 0, space = 0, special = 0;
   char sentence[100];
   char space1 = ' ';
   int i;

   cout<< "Enter The String : ";
   cin.getline(sentence, 80);

   i = 0;
   while (sentence[i] != '\0') {
      if (sentence[i] >= 'A' && sentence[i] <= 'Z')
         upper++;
      if (sentence[i] >= 'a' && sentence[i] <= 'z')
         lower++;
      if (sentence[i] == space1)
         space++;
      if((sentence[i]>=33&&sentence[i]<=47)||(sentence[i]>=58&&sentence[i]<=64)||(sentence[i]>=91&&sentence[i]<=96))
        special++;
      i++;
   }

   cout<< "No. of Capital letters:    "<<upper<<endl;
   cout<< "No. of Small letters:      "<<lower<<endl;
   cout<< "No. of Spaces:             "<<space<<endl;
   cout<< "No. of Special characters: "<<special<<endl;
   return (0);
}

2 Answers2

3

You can use the standard library for this:

std::count_if( InputIt first, InputIt last, UnaryPredicate p )

Will return how often the predicate p returned true while iterating from first to last.

Then you can pass:

  • ::isupper
  • ::islower
  • ::isspace
  • [&] (char c) { return ! ::isspace(c) && ! ::isalnum(c); }

as the predicate.

Make sure you use the globally scoped versions and not ones in the std namespace (::isupper and not std::isupper) for the first three functions.

Example:

std::string s("Hello World!");

std::cout << "Upper case characters: " << std::count_if(s.begin(), s.end(), ::isupper) << std::endl;

std::cout << "Lower case characters: " << std::count_if(s.begin(), s.end(), ::islower) << std::endl;

std::cout << "Whitespace characters: " << std::count_if(s.begin(), s.end(), ::isspace) << std::endl;

std::cout << "Special characters: " << std::count_if(s.begin(),
                                                     s.end(),
                                                     [&] (char c)
                                                     { return ! ::isspace(c) && ! ::isalnum(c); })
          << std::endl;

Outputs:

Upper case characters: 2
Lower case characters: 8
Whitespace characters: 1
Special characters: 1

As an alternative to the lambda for special characters: "You can probably use ::ispunct for the special character case, although it might return True for ASCII values >0x7F." (See comment by mhawke)

Peter Goldsborough
  • 1,336
  • 15
  • 20
0

The simplest way is just move loop to function and use references to counters:

#include<iostream>

using namespace std;

void cntChars(char * sentence, int& upper, int& lower, int& space, int& special)
{
   int i = 0;
   while (sentence[i] != '\0') {
      if (sentence[i] >= 'A' && sentence[i] <= 'Z')
         upper++;
      if (sentence[i] >= 'a' && sentence[i] <= 'z')
         lower++;
      if (sentence[i] == space1)
         space++;
      if((sentence[i]>=33&&sentence[i]<=47)||(sentence[i]>=58&&sentence[i]<=64)||(sentence[i]>=91&&sentence[i]<=96))
        special++;
      i++;
   }
}


int main()
{
   int upper = 0, lower = 0, space = 0, special = 0;
   char sentence[100];
   char space1 = ' ';
   int i;

   cout<< "Enter The String : ";
   cin.getline(sentence, 80);

   cntChars(sentence, upper, lower, space, special);  // call function

   cout<< "No. of Capital letters:    "<<upper<<endl;
   cout<< "No. of Small letters:      "<<lower<<endl;
   cout<< "No. of Spaces:             "<<space<<endl;
   cout<< "No. of Special characters: "<<special<<endl;
   return (0);
}

But detection of character type is really to be improved.

EDIT:

If you really want 4 independent functions, see the following one and make three more by analogy:

#include <cctype>

int cntUpper(char * sentence)
{
   int upper = 0;
   int i = 0;
   while (sentence[i] != '\0') {
      if (isupper(sentence[i])) 
      // same to if (sentence[i] >= 'A' && sentence[i] <= 'Z')
      {
         upper++;
      }
      i++;
   }
   return upper;
}

. . .
// in the main()
cin.getline(sentence, 80);

cout<< "No. of Capital letters:    "<< cntUpper(sentence) <<endl;
VolAnd
  • 6,210
  • 3
  • 20
  • 41