Write a program to count total number of alphabets, digits or special characters in a string using loop

Write a program to count total number of alphabets, digits or special characters in a string using loop
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void main()
  5. {
  6.     char str[100];
  7.     int alph, digit, splch;
  8.     int len;
  9.         alph=0;
  10.         digit=0;
  11.         splch=0;
  12.         len=0;
  13.    
  14.     printf("Count total number of alphabets, digits and special characters :\n");
  15.     printf("Input the string : ");
  16.     gets(str);
  17.     len=strlen(str);
  18.  
  19.     for(int i=0; i < len; i++) {
  20.         if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
  21.             alph++;
  22.         else if (str[i] >= '0' && str[i] <= '9')
  23.             digit++;
  24.         else
  25.             splch++;
  26.     }
  27.     //Check each character of string
  28.     printf("Number of Alphabetics in the string is : %d\n", alph);
  29.     printf("Number of Digits in the string is : %d\n", digit);
  30.     printf("Number of Special characters in the string is : %d\n\n", splch);
  31. }

একটি মন্তব্য পোস্ট করুন