Checking for Small and Capital Letters in C using the Ternary Operator
This is a C program that prompts the user to enter a character, and then uses the ternary operator (? :) to check if it is a small letter or a capital letter. If it is a small letter, it prints a message saying " [value of c] is Small Letter" and if it is a capital letter, it prints a message saying " [value of c] is Captial Letter". If the entered character is not a letter (for example a digit or a special character) the program will print "Others".
- The #include<stdio.h> is a preprocessor directive that includes the contents of the standard input-output library in the program.
- The int main() function is the starting point of the program execution. Inside the main function, a character variable c is declared.
- printf() is used to display the prompt for the user to enter a character, and scanf() is used to get the input and store it in the variable 'c'.
- The ternary operator c>=97&&c<=122?printf("%c is Small Letter",c):c>=65&&c<=127?printf("%c is Captial Letter",c):printf("Others"); is used to check if the entered character is a small letter or a capital letter.
- The first condition c>=97&&c<=122 returns true if the entered character is between 'a' and 'z' in the ASCII table, which represents small letters in C.
- The second condition c>=65&&c<=127 returns true if the entered character is between 'A' and 'Z' in the ASCII table, which represents capital letters in C.
- If the entered character is a small letter, the first part of the ternary operator, printf("%c is Small Letter",c), will execute, if it's a capital letter the second part of the ternary operator printf("%c is Captial Letter",c) will execute, otherwise the third part of the ternary operator printf("Others") will execute.
Finally, the return 0 statement is used to indicate the successful execution of the program. The return value of 0 is a convention used to indicate that the program has executed correctly.
Source Code
#include <stdio.h>
int main()
{
char c;
printf("\nEnter The Character : ");
scanf("%c",&c);
c>=97&&c<=122?printf("%c is Small Letter",c):c>=65&&c<=127?printf("%c is Captial Letter",c):printf("Others");
return 0;
}
To download raw file
Click Here
Output
Enter The Character : a
a is Small Letter