C Program to count alphabets, digits and special characters in a string

Category: C ProgramTags: cprogram string

C Program to count alphabets, digits and special characters in a string using loop. How to find number of alphabets, digits and special characters in a string. Logic to find count alphabets, digits and special characters in a string.

C Program to count alphabets, digits and special characters in a string

#include <stdio.h>

void main()
{
  char str[50];
  int chars = 0, digits = 0, spec_chars = 0, i;

  printf("Enter string: ");
  gets(str);

  for (i = 0; str[i] != '\0'; i++)
  {
    if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
      chars++;
    else if (str[i]>='0' && str[i]<='9')
      digits++;
    else
      spec_chars++;
  }

  printf("\nNumber of alphabets: %d\n", chars);
  printf("Number of digits: %d\n", digits);
  printf("Number of special characters: %d\n", spec_chars);
}

Output

Output

Enter string: I Love my country 3000 times.

Number of alphabets: 19
Number of digits: 4
Number of special characters: 6