C Program to Count Alphabets, Digits and Special Characters in a String

In this tutorial, i am going to show you how to count a number of alphabets, digit, or special characters in a string with the help of for loop, ASCII value, while loop, recurison, pointer and functions in the c program.

All C Programs to Count Alphabets, Digits and Special Characters in a String

  • C Program to Count Alphabets, Digits and Special Characters in a String using For Loop and Ascii value
  • C Program to Count Alphabets, Digits and Special Characters in a String using Function
  • C Program to Count Alphabets, Digits and Special Characters in a String using Recursion
  • C Program to Count Alphabets, Digits and Special Characters in a String using While Loop and Pointer

C Program to Count Alphabets, Digits and Special Characters in a String using For Loop and Ascii Value

#include <stdio.h>
#include <string.h>
 
int main()
{
    char s[1000]; 
    int i,alphabets=0,digits=0,specialcharacters=0;
 
    printf("Enter  the string : ");
    gets(s);
     
    for(i=0;s[i];i++)  
    {
        if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122) )
          alphabets++;
        else if(s[i]>=48 && s[i]<=57)
         digits++;
        else
         specialcharacters++;
 
 	}
 	
     
    printf("Alphabets = %d\n",alphabets);
    printf("Digits = %d\n",digits);
    printf("Special characters = %d", specialcharacters);
    
 
    return 0;
}

The result above c program; as follows:

Enter  the string : lara@^&*%4h34
Alphabets = 5
Digits = 3
Special characters = 5

C Program to Count Alphabets, Digits and Special Characters in a String using Function

#include <stdio.h>
#include <string.h>
 
void stringcount(char *s)
{
	int i,alphabets=0,digits=0,specialcharacters=0;
	for(i=0;s[i];i++)  
    {
        if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122) )
          alphabets++;
        else if(s[i]>=48 && s[i]<=57)
         digits++;
        else
         specialcharacters++;
 
 	}
    printf("Alphabets = %d\n",alphabets);
    printf("Digits = %d\n",digits);
    printf("Special characters = %d", specialcharacters);
 
 	
}
int main()
{
 
    char s[1000];  
  
    printf("Enter  the string: ");
    gets(s);
    
 
    stringcount(s);
     
     
}

The result above c program; as follows:

Enter  the string: name@#^&*$(345
Alphabets = 4
Digits = 3
Special characters = 7

C Program to Count Alphabets, Digits and Special Characters in a String using Recursion

 #include <string.h>
 
void stringcount(char *s)
{
	static int i,alphabets=0,digits=0,specialcharacters=0;
	if(!s[i])
    {
    	printf("Alphabets = %d\n",alphabets);
        printf("Digits = %d\n",digits);
        printf("Special characters = %d", specialcharacters);
        return;
    }
    else
    {
    	
    	if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122) )
          alphabets++;
        else if(s[i]>=48 && s[i]<=57)
         digits++;
        else
         specialcharacters++;
        i++;
         stringcount(s);
    }  
}
int main()
{
    char s[1000];  
  
    printf("Enter  the string: ");
    gets(s);
    
 
    stringcount(s);
     
     
 
 }

The result above c program; as follows:

Enter  the string: hy#$%^&76555
Alphabets = 2
Digits = 5
Special characters = 5

C Program to Count Alphabets, Digits and Special Characters in a String using While Loop and Pointer

#include <string.h>
 
 
int main()
{
    char s[1000],*p;  
    int alphabets=0,digits=0,specialcharacters=0;
 
    printf("Enter  the string: ");
    gets(s);
    
    p=s;
 
 	while(*p)  
    {
    	if( (*p>=65 && *p<=90) || (*p>=97 && *p<=122 ) )
          alphabets++;
        else if(*p>=48 && *p<=57)
         digits++;
        else
         specialcharacters++;
         
        p++; 
 	}
 	printf("Alphabets = %d\n",alphabets);
    printf("Digits = %d\n",digits);
    printf("Special characters = %d", specialcharacters);
  
 	     
     
    
    
}

The result above c program; as follows:

Enter  the string: test%^tes543
Alphabets = 7
Digits = 3
Special characters = 2

More C Programming Tutorials

Leave a Comment