A character is stored in memory in one byte according to a specific encoding. Ma
ID: 3880992 • Letter: A
Question
A character is stored in memory in one byte according to a specific encoding. Many machines use ASCII character codes. A character is considered to have the integer value corresponding to its ASCII encoding. Nonprintable control characters also have ASCII values. For example:
ASCII Integer Value
Name of Character
8
backspace
9
tab
10
newline
12
formfeed
13
carriage return
Using the table above, write a switch statement that displays the name of the character, given its ASCII integer value. If the value is not in the table, display the message "unknown character." Assume that the ASCII integer is the value of the variable intVal.
2. Write a multiple-alternative if statement equivalent to the switch statement described in question 1.
ASCII Integer Value
Name of Character
8
backspace
9
tab
10
newline
12
formfeed
13
carriage return
Explanation / Answer
Switch statements uses the case keyword to execute particular pre-defined statements for each case. Here is the switch statement to display the name of the character. The statement prints "unknown charcater" if it's ASCII value is not present in the table.
switch (intVal)
{
case 8 :
printf("backspace");
break;
case 9 :
printf("tab");
break;
case 10 :
printf("newline");
break;
case 12:
printf("formfeed");
break;
case 13 :
printf("carraige return");
break;
default:
printf("unknown character);
}
2. if statement to impement the above functionality:
if (intval == 8)
printf("backspace");
else if (intVal ==9)
printf("tab");
else if (intVal ==10)
printf("newline");
else if (intVal ==12)
printf("formfeed");
else if (intVal ==13)
printf(" carriage return");
else
printf("unknown character);
The codes are self explanatory
PLEASE UPVOTE IF THE ANSWER IS HELPFUL
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.