String

Program to Count Uppercase, Lowercase,Special character and Numeric values in C

Program to Count Uppercase, Lowercase, Special character and Numeric values

In this article, we will discuss the concept of the Program to Count Uppercase, Lowercase, Special character and Numeric values in C language

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

Count Uppercase, Lowercase, Special character and Numeric values

Program to Count Uppercase, Lowercase, Special character and Numeric values in C

Count Uppercase, Lowercase, Special character and Numeric values using for loop

The program allows the user to enter a String and then it counts and display the total number of upper case, lower case, numeric values and special characters 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 upper=0,lower=0,num=0,special=0;;
    printf("Please enter the string \n");
    gets(str);
    for(i=0; str[i] != '\0'; i++){
        //check for uppercase
            if(str[i]>='A' && str[i]<='Z') {
                upper++;
    }else if(str[i]>='a' && str[i]<='z') {//check lower case
                lower++;
    }else if(str[i]>='1' && str[i]<='9') { //check number
                num++;
    }
    else{
        special++;
    }
    }
    printf("\nUpper case letters: %d",upper);
    printf("\nLower case letters: %d",lower);
    printf("\nNumbers: %d",num);
    printf("\nSpecial characters: %d",special);
    getch();
    return 0;
}

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

Please enter the string
To_Learn_Code:code4coding.com

Upper case letters: 3
Lower case letters: 21
Numbers: 1
Special characters: 4


Approach

  1. Declare a String variable as character array char str[100];
  2. Declare and initialize an integer variables as int i, upper=0,lower=0,num=0,Special=0;
  3. The user asked to enter a string to count upper case, lower case, Numeric, Special characters
  4. The given string is stored in the variable str;
  5. A for-loop is used to count total characters of the given string.
  6. It is initialized as i=0, checks the test-expression 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’), if it is true,  The upper becomes upper + 1(upper=upper+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 lower becomes lower + 1(lower=lower+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 num becomes num+1(num=num+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 upper case, lower case, Numeric, Special characters of the given string

 

 

Count Uppercase, Lowercase, Special character and Numeric values using while loop

The program allows the user to enter a String and then it counts and display the total number of upper case, lower case, numeric values and special characters 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 upper=0,lower=0,num=0,special=0;;
    printf("Please enter the string \n");
    gets(str); 
    i=0;
    while(str[i] != '\0'){
        //check for uppercase
            if(str[i]>='A' && str[i]<='Z') {
                upper++;
                //ceck lower case
    }else if(str[i]>='a' && str[i]<='z') {
                lower++;
                //Check numeric
    }else if(str[i]>='1' && str[i]<='9') {
                num++;
    }
    else{//check special character
        special++;
    }
    i++;
    }
    printf("\nUpper case letters: %d",upper);
    printf("\nLower case letters: %d",lower);
    printf("\nnumbers: %d",num);
    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:Learn_4_Program
Upper case letters: 2
Lower case letters: 23
Numbers: 2
Special characters: 4


Approach

  1. Declare a String variable as character array char str[100];
  2. Declare and initialize integer variables as int i, upper=0,lower=0,num=0,Special=0;
  3. The user asked to enter a string to count upper case, lower case, Numeric, Special characters
  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 test-expression 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’), if it is true,  The upper becomes upper + 1(upper=upper+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 lower becomes lower + 1(lower=lower+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 num becomes num+1(num=num+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 upper case, lower case, Numeric, Special characters of the given string

Count Uppercase, Lowercase, Special character and Numeric values using do-while loop

The program allows the user to enter a String and then it counts and display the total number of upper case, lower case, numeric values and special characters 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 upper=0,lower=0,num=0,special=0;;
    printf("Please enter the string \n");
    gets(str);
    i=0;
    do{
        //check for uppercase
            if(str[i]>='A' && str[i]<='Z') {
                upper++;
                //ceck lower case
    }else if(str[i]>='a' && str[i]<='z') {
                lower++;
                //Check numeric
    }else if(str[i]>='1' && str[i]<='9') {
                num++;
    }
    else{//check special character
        special++;
    }
    i++;
    }while(str[i] != '\0');
    printf("Upper case letters: %d",upper);
    printf("\nLower case letters: %d",lower);
    printf("\nnumbers: %d",num);
    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:Code_1st

Upper case letters: 1

Lower case letters: 18

Numbers: 2

Special characters: 3

 

Approach

  1. Declare a String variable as character array char str[100];
  2. Declare and initialize an integer variables as int i, upper=0,lower=0,num=0,Special=0;
  3. The user asked to enter a string to count upper case, lower case, Numeric, Special characters
  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 test-expression 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’), if it is true,  The upper becomes upper + 1(upper=upper+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 lower becomes lower + 1(lower=lower+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 num becomes num+1(num=num+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 upper case, lower case, Numeric, Special characters 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

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

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

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

 

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

 

Python code to Count Uppercase, Lowercase,Special character and Numeric values
C++ code to Count Uppercase, Lowercase,Special character and Numeric values
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

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

2 days ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

5 days ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 week ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 weeks ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

1 month ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

2 months ago