Search This Blog

Sunday, 15 September 2013

Character Count in a String by C++

Total Characters in a String

#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
  char ch[30];
  int cons=0,alpha=0,digit=0;
  cout<<"Enter a alphanumeric string:";
  cin>>ch;
  int k=strlen(ch);
  for(int i=0;i<k;i++)
  {
     int a=ch[i];
     if(a>=33&&a<=47||a>=58&&a<=64)
     cons++;
     else if(a>=48&&a<=57)
     digit++;
     else
     alpha++;
  }
  cout<<"Given stringis:"<<ch<<endl;
  cout<<"Special character is:"<<cons<<endl;
  cout<<"Digits are:"<<digit<<endl;
  cout<<"characters are:"<<alpha;
  getch();
}
output

No comments:

Popular Posts