Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Example of String
2.
Problem Statement
3.
Algorithm
4.
Implementation 
5.
Frequently Asked Questions
5.1.
Why did we utilize the gets() method in the above code?
5.2.
What do you mean by the string in C programming?
5.3.
Why isn't string used in C?
5.4.
When a string finishes, how does C know?
5.5.
How to find string length in C?
5.6.
In C, how long is a string?
6.
Conclusion
Last Updated: Mar 27, 2024

C Program to Count Uppercase, Lowercase, and Special Characters in the String

Author Aditya kumar
2 upvotes
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this article, we'll look at a C program to count uppercase, lowercase, and special characters in the string, the approach employed, and the time and space complexity. 

Let's define what a string means before we get into the C program to count uppercase, lowercase, and special characters in the string.

Strings: Strings are a one-dimensional array of characters with the null character '0' at the end. On the other hand, a null-terminated string contains the characters that make up the string, followed by a null.

A sequence of characters is referred to as a string. In C, the char data type represents a single character. If you wish to use a string in your program, an array of characters is the way to go.

Example of String

char str[] = "A String in C";

Problem Statement

Write a program that counts the number of Lowercase characters, Uppercase characters, and Special characters in a string.

For example,

Input Output

#CoDinG@ninJAs

 

Upper case letters: 5

Lower case letters: 7

Special Characters: 2

*coDINgNinJas*

Upper case letters: 5

Lower case letters: 7

Special Characters: 2

Also see: C Static Function and  Tribonacci Series

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Algorithm

  1. Declare a String variable with the char str[100] (a character array).
     
  2. Declare and initialize an integer variable with the following values: int i, upper=0, lower=0, special=0.
     
  3. The user was requested to type a string with upper and lower case letters and special characters.
     
  4. The variable str is used to store the specified string.
     
  5. A for-loop counts the total number of characters in a string.
     
  6. It starts with i=0, checks whether str[i]!= '\0' in the test expression, and then executes the loop until the supplied condition is met.
     
  7. If if(str[i]>='A' && str[i]='Z') is true, use an if condition to test it. Upper is changed to upper+1 (or upper=upper+1).
     
  8. If it is false, control passes to the else-if portion, which evaluates the test expression of else-if. If true, control passes to the else-if part, which examines the test expression of else-if. Lower becomes lower+1 (or lower=lower+1).
     
  9. If it is false, control passes to the other part, which performs the other part's instructions. The special becomes special+1.
     
  10. Finally, the program displays the total amount of upper, lower, and special characters in the given string.

 

You can also read about dynamic array in c, Strong number in c

Implementation 

/* C Program to Count Uppercase, Lowercase, and Special Characters in String */
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];
    int i;
    int upper=0,lower=0,special=0;;
    printf("Please enter the string \n");
    gets(str);
    
    // For Uppercase
    for(i=0; str[i] != '\0'; i++){
            if(str[i]>='A' && str[i]<='Z') {
                upper++;
    }
    
    // For Lowercase
    else if(str[i]>='a' && str[i]<='z') { 
               lower++;
    }
   
    // For special
    else{
        special++;
    }
    }
    printf("\nUpper case letters: %d",upper);
    printf("\nLower case letters: %d",lower);
    printf("\nSpecial characters: %d",special);
    getch();
    return 0;
}

 

Output (Example 1)


Output (Example 2)

Time Complexity

Time Complexity is O(n), where n is the length of the string.

for(i=0; str[i] != '\0'; i++){
    // For Uppercase
            if(str[i]>='A' && str[i]<='Z') {
                upper++;
    }
    
   // For Lowercase
    else if(str[i]>='a' && str[i]<='z') { 
               lower++;
    }
    
   // For special
    else{
        special++;
    }


As we can see in this code for loop is starting from 0 to str[i]!=’\0’, this loop is going up to when str[i]!=’\0’, in other words, its meaning is if the string isn't at the end. So, it will check up to n times.

Space Complexity

O(1), because if we will check the above code we find that our algorithm is not consuming any space to store data or to remember variables that we had declared in our code.

You can practice by yourself with the help of a C online compiler.

Frequently Asked Questions

Why did we utilize the gets() method in the above code?

The char *gets(char *str) function in the C library reads a line from stdin and stores it in the str string. It comes to a halt when either the newline character or the end-of-file character is read, whichever comes first.

What do you mean by the string in C programming?

A string in C programming is a sequence of characters that ends with the null character \0. char c[] = "c string" for example; By default, the compiler appends a null character \0 to the end of a sequence of characters wrapped in double quotation marks.

Why isn't string used in C?

In C, there is no such thing as a string type. You must make use of char arrays. Your code won't work since the array's size should allow for the entire array plus one more zero-terminating character.

When a string finishes, how does C know?

In C, strings are represented as character arrays. The null character, which is just the character with the value 0, indicates the end of the string.

How to find string length in C?

The strlen() method determines how long a string is. The strlen() function returns the length of a string as an argument.

In C, how long is a string?

In C, a string is essentially a collection of characters. The next line declares an array that can hold up to 99 characters of text. char str[100]; char str[100]; char str[100]; It stores characters in the order you'd expect: str[0] is the first character, str[1] is the second, and so on.

Conclusion

The implementation of the C program to count uppercase, lowercase, and special characters in the string is shown in this article. We'd also seen the written program's output on some random data. We have discussed its time and space complexity as well. This type of problem can be asked in an interview to test your knowledge in string.

We hope this blog has helped you enhance your knowledge regarding the C program to count uppercase, lowercase, and special characters in the string. If you want to learn more about C programs, visit the given links below:


Check out this problem - Longest String Chain

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; look at the Top 150 Interview Puzzles interview experiences, and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Happy Learning Ninja!

Previous article
Perfect Number Program in C
Next article
C Program to arrange the string in alphabetical order
Live masterclass