1. The data type char and the data type __ are often interchangeable in C++. a)
ID: 3595899 • Letter: 1
Question
1. The data type char and the data type __ are often interchangeable in C++.
a) double
b) float
c) int
d) string
2. What will happen if a char is cout'd as an int?
a) the character will be displayed
b) the ASCII value of the character will be displayed
c) it will not compile
d) it will produce a run-time error
3) What is the ASCII value of the character '1'?
4) What is the ASCII value of the character 'c'?
5) Write a cout statement to display the ASCII value of the character stored in a char variable called ch. Assume that ch has been properly declared and contains a value.
6) Explain what tolower() does to a character. The answer should include the argument(s) that are passed to tolower() (if any) and what it returns.
7) Write a C++ statement that takes the digit character in the char variable ch and changes it to its integer representation and saves the result in the integer variable intVal. So if ch contains '4', then intVal should contain the value 4. Assume that ch and intVal have been properly declared.
8) Write a switch statement to print out a string depending on the value of a char variable called ch. If:
-ch is 'a' print "excellent"
-ch is 'b' print "good"
-ch is 'c' print "ok"
-if ch is anything else, print "invalid"
Assume that ch has been properly declared and contains a value.
Explanation / Answer
1) string
2) the ASCII value of the character will be displayed
3) 49
4) 99
5) char ch='A';
cout<<int (ch);
6) char ch='D';
tolower(ch); // it converts character D into small d
Converts 'D' to its lowercase equivalent 'd' if 'D' is an uppercase letter and has a lowercase equivalent. If no such conversion is possible, the value returned is 'D' unchanged.
7)
#include<iostream>
using namespace std;
int main()
{
char ch='4';
int intval;
intval=ch-48;
cout<<intval;
return 0;
}
8)
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a character: ";
cin>>ch;
switch(ch)
{
case 'a': cout<<"excellent";
break;
case 'b': cout<<"good";
break;
case 'c': cout<<"ok";
break;
default: cout<<"invalid";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.