Write C++ program to count number of alphabets, digits and special characters in string


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. #define MAX_SIZE 100 //Maximum size of the string
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. char string[MAX_SIZE];
  10. int alphabets, digits, specialchars, i;
  11.  
  12. alphabets = digits = specialchars = i = 0;
  13.  
  14. cout<<"Enter any string: ";
  15. cin>>string;
  16.  
  17. while(string[i]!='\0')
  18. {
  19. if((string[i]>='a' && string[i]<='z') || (string[i]>='A' && string[i]<='Z'))
  20. {
  21. alphabets++;
  22. }
  23. else if(string[i]>='0' && string[i]<='9')
  24. {
  25. digits++;
  26. }
  27. else
  28. {
  29. specialchars++;
  30. }
  31.  
  32. i++;
  33. }
  34.  
  35. cout<<"Total Alphabets: "<<alphabets<<endl;
  36. cout<<"Total Digits: "<<digits<<endl;
  37. cout<<"Total Special characters: "<<specialchars<<endl;
  38.  
  39. }
  40.  
  41.  
  42.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts