Top

[C] Character counter


C++ Code
+ 0 likes
Please Register to submit score.
Bookmark and Share
Average Score  0.0 (of 0 scores)
Date Added  Feb 11, 2009
Last Updated  Feb 11, 2009
Tags  c  character  cpp  cunter 

Description

This little program will gather all entered characters and determine how many numeric characters there are, alpha characters and special punctuation chars, including spaces!

Grab the Code

/* Programmer: Boris Kuzmanov alias A^1^T^E^A^M (Gravedigger) */
#include <stdio.h>
#include <ctype.h>
 
int main()
{
      char ch;
      int num_ch, num_num, spec_num;
 
      num_ch = 0;
      num_num = 0;
      spec_num = 0;
 
      printf("Print characters: ");
 
          while((ch = getchar()) != '\n')
             {
 
                    if(isalpha(ch))
                         num_ch++;
                    else if(isdigit(ch))
                         num_num++; 
                    else if(ispunct(ch))
                         spec_num++;
                    else if(isspace(ch))
                         spec_num++;
             }
 
          printf("\nThere are %d letters in the string!", num_ch);
          printf("\nThere are %d numbers in the string!", num_num);
          printf("\nThere are %d special characters in the string!\n", spec_num);
 
      system("Pause");
      return 0;
}
 
 

Comments

  (1)  RSS
NIGathan
Comments: 208
 
C++ Snippet:  [C] Character counter
Posted on Apr 2, 2009 4:20 am
rofl at the 'cu​nter' tag. Nice code, happen to know if the isalpha(), isdigit(), ..etc functions are in a C++ header?

Nvm I found it, its in locale

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  

Bottom