String

C code to Count Alphabets, Numeric, Special character and Space

C code to Count Alphabets, Numeric, Special character and Space

In this article, we will discuss the concept of the C code to Count Alphabets, Numeric, Special character and Space in C language

In this post, we are going to learn how to count the total number of Alphabets, Numeric, Special character and Space of the given string in C programming language

Count Alphabets, Numeric, Special character and Space

C code to Count Alphabets, Numeric, Special character and Space

Count Alphabets, Numeric, Special character and Space using for loop

The program allows the user to enter a String and then it counts and display the total number of Alphabets, Numeric, Special character and Space of the given string using for loop in C programing language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];
    int i;
    int Alphabets=0,num=0,special=0,space=0;
    printf("Please enter the string \n");
    gets(str);
    for(i=0; str[i] != '\0'; i++){
        
            if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')) {//Check Alphabets
                Alphabets++;
    }else if(str[i]>='0' && str[i]<='9') {//Check numbers
                num++;
    }

    else if(str[i]==' '){//Check space
                space++;
    }
    else{//Find special caracters
        special++;
    }
    }
    printf("\nAlphabets letters: %d",Alphabets);

    printf("\Numbers: %d",num);
      printf("\nSpace: %d",space);
    printf("\nSpecial characters: %d",special);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Please enter the string
code4coding.com 2020

Alphabets letters:13
Numbers:5
Space:1
Special characters:1

 

Approach

  1. Declare a Character array as char str[100];
  2. Declare and initialize integer variables as Alphabets=0,num=0,space=0,Special=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A for loop is used to count every total of the given string.
  6. It is initialized as i=0, checks the condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
  7. Use an if condition to test if((str[i]>=’A’ && str[i]<=’Z’) || (str[i]>=’a’ && str[i]<=’z’)), if it is true,  The Alphabets becomes Alphabets + 1( Alphabets = Alphabets +1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The num becomes num+1(num=num+1);
  9. When it is false, control moves to the next else-if part and evaluates the test-expression of else-if. if it is true The space becomes space+1(space=space+1);
  10. When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
  11. Finally, the program displays the total number of the Alphabets, Numeric, Special character and Space of the given string

 

Count Alphabets, Numeric, Special character and Space using while loop

The program allows the user to enter a String and then it counts and display the total number of Alphabets, Numeric, Special character and Space of the given string using While loop in C programing language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];
    int i;
    int Alphabets=0,num=0,special=0,space=0;
    printf("Please enter the string \n");
    gets(str);
    i=0;
    while(str[i] != '\0'){
        //check for Alpabets
            if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')) {
                Alphabets++;
    }else if(str[i]>='0' && str[i]<='9') {//Check numbers
                num++;
    }

    else if(str[i]==' '){//Check spave
                space++;
    }
    else{
        special++;//Check special characters
    }
    i++;
    }
    printf("\nAlphabets letters: %d",Alphabets);

    printf("\nnumbers: %d",num);
      printf("\nSpace: %d",space);
    printf("\nSpecial characters: %d",special);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Please enter the string
Code For You:code4coding.com

Alphabets letters:23
Numbers:1
Space:2
Special characters:2


Approach

  1. Declare a Character array as char str[100];
  2. Declare and initialize integer variables as Alphabets=0,num=0,space=0,Special=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A while loop is used to count total characters of the given string.
  6. It is initialized as i=0, checks the condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
  7. Use an if condition to test if((str[i]>=’A’ && str[i]<=’Z’) || (str[i]>=’a’ && str[i]<=’z’)), if it is true,  The Alphabets becomes Alphabets + 1( Alphabets = Alphabets +1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The num becomes num+1(num=num+1);
  9. When it is false, control moves to the next else-if part and evaluates the test-expression of else-if. if it is true The space becomes space+1(space=space+1);
  10. When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
  11. Finally, the program displays the total number of the Alphabets, Numeric, Special character and Space of the given string

 

Count Alphabets, Numeric, Special character and Space using do-while loop

The program allows the user to enter a String and then it counts and display the total number of Alphabets, Numeric, Special character and Space of the given string using do-while loop in C programing language

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];
    int i;
    int Alphabets=0,num=0,special=0,space=0;
    printf("Please enter the string \n");
    gets(str);
    i=0;
    do{
        //check for uppercase
            if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')) {//Check Alphabets
                Alphabets++;
    }else if(str[i]>='0' && str[i]<='9') {//Check numeric
                num++;
    }

    else if(str[i]==' '){//Check space
                space++;
    }
    else{//Check special
        special++;
    }
    i++;
    }while(str[i] != '\0');
    printf("\nAlphabets letters: %d",Alphabets);

    printf("\nnumbers: %d",num);
      printf("\nSpace: %d",space);
    printf("\nSpecial characters: %d",special);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Please enter the string
Code with me:code4coding.com

Alphabets letters:23
Numbers:1
Space:2
Special characters:2


Approach

  1. Declare a Character array as char str[100];
  2. Declare and initialize integer variables as Alphabets=0,num=0,space=0,Special=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A do-while loop is used to count total characters of the given string.
  6. It is initialized as i=0, checks the condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
  7. Use an if condition to test if((str[i]>=’A’ && str[i]<=’Z’) || (str[i]>=’a’ && str[i]<=’z’)), if it is true,  The Alphabets becomes Alphabets + 1( Alphabets = Alphabets +1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The num becomes num+1(num=num+1);
  9. When it is false, control moves to the next else-if part and evaluates the test-expression of else-if. if it is true The space becomes space+1(space=space+1);
  10. When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
  11. Finally, the program displays the total number of the Alphabets, Numeric, Special character and Space of the given string

 

 

Suggested for you

for loop in C language

while loop in C language

do-while loop in C  language

 

Similar post

Java program to count the total number of  upper case lower case numeric and special character

C program to count the total number of  upper case lower case numeric and special character

Python program to count the total number of  upper case lower case numeric and special character

C++ program to count the total number of  upper case lower case numeric and special character

 

Java program to count the total number of characters in the given string including space

C program to count the total number of characters in the given string including space

Python program to count the total number of characters in the given string including space

 

 

C++ code to Count Alphabets, Numeric, Special character and Space in a string
Python program to Count words, characters and space of a string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago