Using Conditional / Ternary Operator determine, whether a character entered through the keyboard is a Special Symbol or not.
Note: ASCII values start from 0 to 255. So we have total of 266 ASCII values.
ASCII Code for Special Symbols |
---|
0 – 47 |
58 – 64 |
91 – 96 |
123 – 255 |
Also Check:
C Program To Find Lowercase Alphabet or Not using Conditional Operator
Related Read:
Relational Operators In C
Logical Operators In C
Ternary Operator / Conditional Operator In C
C Program To Print All ASCII Characters and Code
Ternary / Conditional Operator General Form
(expression_1) ? (expression_2) : (expression_3);
expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.
Ternary operator / Conditional Operator can be assumed to be shortened way of writing an if-else statement.
Expected Output for the Input
User Input:
Enter a Character
$
Output:
Character Entered Is a Special Symbol
Logic To Find Special Symbol or Not using Conditional Operator
Using Conditional Operator we write the condition, if user entered character is in between or equal to ASCII values 0 – 47 or 58 to 64 or 91 to 96 or greater than or equal to 123. We use Relational Operator and Logical Operators to accomplish the task.
If the condition in expression_1 is true, then whatever code is present in expression_2 gets executed. If condition is expression_1 is false then the code present in expression_3 gets executed.
Video Tutorial: C Program To Find Character is Special Symbol or Not using Conditional Operator
[youtube https://www.youtube.com/watch?v=LDxdmrCRqCU]
Source Code: C Program To Find Special Symbol or Not using Conditional Operator
#include<stdio.h> int main() { char ch; printf("Enter a Character\n"); scanf("%c", &ch); ( (ch >= 0 && ch <= 47) || (ch >= 58 && ch <= 64) || (ch >= 91 && ch <= 96) || (ch >= 123) ) ? printf("Character Entered Is a Special Symbol\n") : printf("Character Entered Is not a Special Symbol\n"); return 0; }
Output 1:
Enter a Character
#
Character Entered Is a Special Symbol
Output 2:
Enter a Character
a
Character Entered Is not a Special Symbol
Output 3:
Enter a Character
Z
Character Entered Is not a Special Symbol
Output 4:
Enter a Character
5
Character Entered Is not a Special Symbol
Output 5:
Enter a Character
$
Character Entered Is a Special Symbol
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert