Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write C program using isupper() function
In C, the isupper() function is used to check if a character is an uppercase letter (A-Z). It is defined in the ctype.h header file and returns a non-zero value if the character is uppercase, otherwise returns 0.
Syntax
int isupper(int ch);
Parameters: ch − The character to be checked (passed as int).
Return Value: Non-zero if the character is uppercase, 0 otherwise.
Example 1: Counting Uppercase Letters Using isupper()
This program counts the total number of uppercase letters in a string using the isupper() function −
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char string[100];
int counter = 0;
int i;
printf("Enter the string: ");
fgets(string, sizeof(string), stdin);
for (i = 0; string[i] != '\0'; i++) {
if (isupper(string[i])) {
counter++;
}
}
printf("Capital letters in string: %d<br>", counter);
return 0;
}
Enter the string: TutoRialsPoint CPrograMMing Capital letters in string: 7
Example 2: Manual Uppercase Check Without isupper()
This program demonstrates how to count uppercase letters by manually checking ASCII values without using isupper() −
#include <stdio.h>
int main() {
char string[100];
int upper = 0;
int i = 0;
printf("Enter the string: ");
fgets(string, sizeof(string), stdin);
while (string[i] != '\0') {
if (string[i] >= 'A' && string[i] <= 'Z') {
upper++;
}
i++;
}
printf("Uppercase letters: %d<br>", upper);
return 0;
}
Enter the string: TutOrial Uppercase letters: 2
Example 3: Character-by-Character Analysis
This program shows individual uppercase characters found in the string −
#include <stdio.h>
#include <ctype.h>
int main() {
char string[100];
int count = 0;
printf("Enter the string: ");
fgets(string, sizeof(string), stdin);
printf("Uppercase letters found: ");
for (int i = 0; string[i] != '\0'; i++) {
if (isupper(string[i])) {
printf("%c ", string[i]);
count++;
}
}
printf("\nTotal uppercase letters: %d<br>", count);
return 0;
}
Enter the string: Hello World Uppercase letters found: H W Total uppercase letters: 2
Key Points
- The
isupper()function only works with ASCII characters A-Z. - Always include
ctype.hheader when usingisupper(). - Use
fgets()instead ofgets()for safer string input. - Manual checking uses ASCII values: uppercase letters range from 65 to 90.
Conclusion
The isupper() function provides a reliable way to identify uppercase characters in C. It simplifies character validation and makes code more readable compared to manual ASCII range checking.
