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

C program to count alphabet, digit, or special characters in a string; Through this tutorial, we will learn how to count a number of alphabets, digit, or special characters in a string using for loop, ASCII value, while loop, recurison, pointer and functions in the c program.

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

  • 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 output above c program; as follows:

Enter  the string : tutsmake@^&*%4h34
Alphabets = 9
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 output 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 output 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 output above c program; as follows:

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

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *