Question

Write a function to show a character's description. write a function that takes an unsigned character...

Write a function to show a character's description.

write a function that takes an unsigned character c as a parameter and prints c's description.

The description of 'a' is just a. But the description of '\n' (the newline character) should be \n, or some other desriptive name, such as newline. Use descriptive names for the tab ('\t') and space characters as well.

For an unprintable character whose integer code is m, use description \m. For example, the character with code 127 will have description "\127".

To check whether a character is printable, #include <cctype>. Then isprint(c) is true if character c is printable and is not a white-space character. Pass isprint a nonnegative integer code or an unsigned character, since it uses the character as an index in an array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include <iostream>

#include <cctype>

using namespace std;

//method to print the description of an unsigned char

void printDescription(unsigned char c){

                //identifying the character

                if(c=='\n'){

                                cout<<"newline"<<endl;

                }else if(c==' '){

                                cout<<"space"<<endl;

                }else if(c=='\t'){

                                cout<<"tab"<<endl;

                }else if(isprint(c)){

                                //printable character, just printing it

                                cout<<c<<endl;

                }else{

                                //not printable, just displaying a \ before the integer code of character

                                int code=c;

                                cout<<"\\"<<code<<endl;

                }

}

int main() {

                //testing the method using multiple test cases

                printDescription('a'); //a

                printDescription('Z'); //Z

                printDescription(' '); //space

                printDescription('\n'); //newline

                printDescription('\t'); //tab

                printDescription(180); // \180

                printDescription(65); //should print 'A'

                printDescription(64); //should print @

                return 0;

}

/*OUTPUT*/

a

Z

space

newline

tab

\180

A

@

Add a comment
Know the answer?
Add Answer to:
Write a function to show a character's description. write a function that takes an unsigned character...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The left-shift operator can be used to pack two character values into an unsigned integer variable....

    The left-shift operator can be used to pack two character values into an unsigned integer variable. Write a program that inputs two characters from the keyboard and passes them to function packCharacters. To pack two characters into an unsigned integer variable, assign the first character to the unsigned variable, shift the unsigned variable left by 8- bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator. The program should output the characters in...

  • Write a C function named  f such that … the prototype of function  f is … unsigned long...

    Write a C function named  f such that … the prototype of function  f is … unsigned long int f ( unsigned long int X, unsigned long int Y ) function  f returns … the nonnegative integer  Z  such that   X 2  +   Y 2  =   Z 2  if such a nonnegative integer  Z  exists zero if there is no nonnegative integer  Z  such that   X 2  +   Y 2  =   Z 2  Example 1 If   X = 3  and   Y = 4 ...

  • Write a C function that receives a character array (string), and it will check the characters...

    Write a C function that receives a character array (string), and it will check the characters of the array to figure out whether the string has a given valid pattern. The required pattern will be given to you. In this function, for instance, you should check if the ascii code of an element of the character array is between a range. The ascii table of all the printable characters will be provided to you. See the last page of this...

  • Write a C function that receives a character array (string), and it will check the characters...

    Write a C function that receives a character array (string), and it will check the characters of the array to figure out whether the string has a given valid pattern. The required pattern will be given to you. In this function, for instance, you should check if the ascii code of an element of the character array is between a range. The ascii table of all the printable characters will be provided to you. See the last page of this...

  • Write the C-code for a function which returns the least common character in an set of...

    Write the C-code for a function which returns the least common character in an set of characters that occurs at least once. (The function's prototype is char least_common(int, unsigned char*);)

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • Assignment 1) Your function will only write the Nth character of the array, supplied as a...

    Assignment 1) Your function will only write the Nth character of the array, supplied as a parameter by the caller. This is an example prototype (although you may want to change the return value type, based on the second twist, below). void writeArrayNthBackward(const char [], int, int, int); The first three arguments serve the same purpose as in the iterative example. The fourth argument (an int) supplies the N for the Nth character to print in the array. If n...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • I need help with this exercise. Than you for the help. Language is C++ 2 Enumeration...

    I need help with this exercise. Than you for the help. Language is C++ 2 Enumeration Data Types Reformat is the shell of a program designed to read characters and process them in the following way Lowercase character Uppercase character Digit Blank Newline Any other character Converts to uppercase and writes the character Writes the character Writes the digit Writes a blank Writes newline Does nothing / Program Reformat reads characters from file DataIn and // writes them to DataOut...

  • Use the function isSeparator(c) above in writing a function that counts the number of words in...

    Use the function isSeparator(c) above in writing a function that counts the number of words in a string. In your solution, consider different cases of the string, such as having few adjacent space characters, tab and newline characters. You may consider that words can be 1 or more alphanumeric character(s), an alphanumeric is either an alphabetical letter (lower or upper case) or a number Hint: The last character of a word is followed by a separator, a new line, or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT