C Program to Count Uppercase, Lowercase, and Special Characters in the String
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 |
Algorithm
- Declare a String variable with the char str[100] (a character array).
- Declare and initialize an integer variable with the following values: int i, upper=0, lower=0, special=0.
- The user was requested to type a string with upper and lower case letters and special characters.
- The variable str is used to store the specified string.
- A for-loop counts the total number of characters in a string.
- 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.
- 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).
- 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).
- If it is false, control passes to the other part, which performs the other part's instructions. The special becomes special+1.
- 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.
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:
- C Program to concatenate two string
- C Program to arrange the string in alphabetical order
- Factorial of a Number
- Difference between c and embedded c
Check out this problem - Longest String Chain
Refer to our guided paths on Codestudio 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!