Start New Discussion within our Software Development Community

For this assignment i must replace all unprintable characters by escape sequences.
So, the program must do the following:

Read each character of the file, and

1) if that character has an escape sequence, print the escape sequence, even if the character is printable.

2) if the character is printable and has no escape sequence, print the character,

3) for all characters not covered by 1) or 2), print the escape sequence 0xhh, where hh is the hex representation of the byte. (You can print the hex value of a byte using the format item %02x in a printf statement. Here x means hex, 2 means print 2 characters, and the 0 means print leading zeros, so it will print 07 for seven, for example.)

When you get a new line character, send the escape code \ and n to the printer, and also send a new line character, represented in your program by its escape code \n, so that the output is divided into lines the same way as the original data.

Any ideas on how to get me started???

If you don't already have it (and you should have it in your textbook) here is a list of valid escape characters. So, for example, if the character read from the file is a 't', then your program will output "\t".

If the character is non-printable -- use isprint() macro to find out -- then output its hex value

Use fopen() to open the file and fgetc() to read a single character. There are other functions that will also read a single character but fgetc() is probably the easiest to use. The program requirements don't state where to output the characters so I assume just print then to the console (or terminal) screen.

To get started ?

#include <stdio.h>

int main()
{
   // your program goes here

   return 0;
}
This article has been dead for over six months. Start a new discussion instead.