• C Program to Count number of Uppercase and Lowercase Letters

    C programming language is widely used for its simplicity and efficiency. It allows developers to write compact and fast code to solve a wide range of problems. In this blog post, we will explore how to write a C program that counts the number of uppercase and lowercase letters in a given input string. This program can be a useful tool for tasks such as analyzing text data or validating password strength. So, let’s dive in and learn how to count uppercase and lowercase letters using C!


    Understanding the Problem

    Before we start writing our program, let’s clarify what we need to achieve. The goal is to take a string as input from the user and count the number of uppercase and lowercase letters present in that string. For example, if the user enters “Hello, World,” the program should output “Uppercase letters: 2” and “Lowercase letters: 8”. To accomplish this, we need to analyze each character in the string and determine whether it is uppercase or lowercase.


    Getting User Input

    To begin, we need to obtain a string from the user. In C, we can use the fgets() function to read a line of input from the user. Let’s start by including the necessary header files and defining a buffer size for our input string:

    #include <stdio.h>
    #define MAX_SIZE 100

    Next, we can write a function to prompt the user for input and retrieve the string they enter:

    void getString(char str[]) {
        printf("Enter a string: ");
        fgets(str, MAX_SIZE, stdin);
    }

    Here, we pass an array str as a parameter to the function and use fgets() to read the input string. The MAX_SIZE constant specifies the maximum number of characters we can read.


    Counting Uppercase and Lowercase Letters

    Now that we have obtained the input string, let’s focus on counting the number of uppercase and lowercase letters. We can accomplish this by iterating over each character in the string and checking its ASCII value. In C, uppercase letters have ASCII values ranging from 65 to 90, while lowercase letters have values ranging from 97 to 122.

    We’ll begin by creating a function named countLetters that takes the input string as a parameter and returns the count of uppercase and lowercase letters as two separate integers:

    void countLetters(const char str[], int *uppercaseCount, int *lowercaseCount) {
        *uppercaseCount = 0;
        *lowercaseCount = 0;
        
        int i = 0;
        while (str[i] != '\0') {
            if (str[i] >= 'A' && str[i] <= 'Z') {
                (*uppercaseCount)++;
            }
            else if (str[i] >= 'a' && str[i] <= 'z') {
                (*lowercaseCount)++;
            }
            
            i++;
        }
    }

    Inside the function, we initialize the uppercaseCount and lowercaseCount variables to zero. Then, we use a while loop to iterate over each character in the string. If the character falls within the ASCII range of uppercase letters, we increment the uppercaseCount variable. Similarly, if the character falls within the ASCII range of lowercase letters, we increment the lowercaseCount variable.


    Putting It All Together

    Now that we have all the necessary components, let’s write a main function to bring everything together:

    int main() {
        char str[MAX_SIZE];
        int uppercaseCount, lowercaseCount;
        
        getString(str);
        countLetters(str, &uppercaseCount, &lowercaseCount);
        
        printf("Uppercase letters: %d\n", uppercaseCount);
        printf("Lowercase letters: %d\n", lowercaseCount);
        
        return 0;
    }

    In our main function, we declare the input string str and the variables uppercaseCount and lowercaseCount to hold the counts. We then call the getString function to get the input string from the user. After that, we call the countLetters function, passing the input string and the addresses of the count variables. Finally, we print the results using printf.


    Testing the Program

    To make sure our program works correctly, let’s run it and test it with different inputs. Here are a few test cases and their expected outputs:

    • Input: “Hello, World”

      Output: Uppercase letters: 2

      Lowercase letters: 8

    • Input: “Programming is Fun”

      Output: Uppercase letters: 1

      Lowercase letters: 15

    • Input: “12345!@#$%”

      Output: Uppercase letters: 0

      Lowercase letters: 0

    You can try running the program with these inputs and see if you get the expected results. Feel free to experiment with different inputs to further validate the program’s functionality.


    Conclusion

    In this blog post, we have learned how to write a C program that counts the number of uppercase and lowercase letters in a given string. We discussed the importance of accurately defining the problem and obtaining user input. We also explored how to analyze each character in the string and determine whether it is uppercase or lowercase.

    By following the step-by-step guide provided in this blog post, you now have the knowledge and skills necessary to implement the “Count number of Uppercase and Lowercase Letters” program in C. Remember, understanding the problem and breaking it down into smaller tasks is crucial when developing any program.

    To further extend your knowledge, You can continue exploring C programming concepts and dive deeper into topics such as string manipulation, character handling, and array-based operations. With practice and perseverance, you’ll become a proficient C programmer and be able to tackle complex tasks with ease.

    So go ahead, keep coding, and enjoy your journey in the vast world of programming!