Write a C program that will read data from standard input one character at a time and output the results to standard output. Roughly, your program should mostly copy the input stream to the output stream, but it should delete all non-ASCII characters, change the case of alphabetic characters, 'fold' long lines, and make browser-friendly adjustments to the characters _, /, :, ?, &, and space. Specifically, for each character you read, do one of the following 5 things:

1) Replace each of the characters '_', '/', ':', '?', '&', and ' ' (the space character) with a three-character sequence, consisting of a '%' character followed by the two hexadecimal digits corresponding to the character's ASCII code. Appendix-I also lists the ASCII character set, but basically, here are the changes:

'_' becomes '%5F'
'?' becomes '%3F'
':' becomes '%3A'
'/' becomes '%2F'
'&' becomes '%26'
' ' becomes '%20'

That is, if the above substitutions were made on an input line of:

google.COM/search?client=SuSE&q=AD.com:Some Pat_tern

...it would be transformed into:

google.COM%2Fsearch%3Fclient=SuSE%26q=AD.com%3ASome%20Pat%5Ftern

2) Interchange the case of the alphabetic letters; that is, if you encounter an 'A', print out an 'a' instead, 'B' becomes 'b', etc. -- and likewise, 'a' becomes 'A', ... 'z' becomes 'Z'. So, (ignoring rule 5 below for the moment)

google.COM/search?client=SuSE&q=AD.com:Some Pat_tern

...will actually be changed by your program into:

GOOGLE.com%2FSEARCH%3FCLIENT=sUse%26Q=ad.COM%3AsOME%20pAT%5FTERN

(note that we are not applying steps 1) and 2) sequentially -- after inserting %2F, it should NOT subsequently become %2f).

3) delete all non-ASCII characters (those with character codes 128 through 255; that is, read it in, but then don't print it if it is not valid ASCII).

4) Any other character not mentioned above (such as digits, '+', control characters, or whatever) is simply printed verbatim.

5) In addition to the above changes, do not let any line get longer than 20 printable characters; that is, if you've printed 20 characters since the last newline, insert an extra newline into the output, so that the next character will be printed in column 1 again. [Note that this is 'folding', not 'truncating' -- characters after the 20th character are still printed, but one line lower down.] The example shown in rule 2 should really produce:

GOOGLE.com%2FSEARCH%
3FCLIENT=sUse%26Q=ad
.COM%3AsOME%20pAT%5F
TERN

[Note that the "%3F" was one of the things that had a newline inserted in it; I recommend first ignoring this complication, and then making this last little tweak after everything else works right.]

Your main program should return a value when it finishes. Return one (1) if the output was unchanged from the input that was read in, and return zero (0) if you made at least one change (either by removing non-ASCII characters, expanding the special web characters, inserting an extra newline, or changing case).

Note that this is consistent with the paradigm used in UNIX: zero is returned when we have a "normal" exit, allowing the program to be used in another way: we could ignore the output of the program and instead just look at the return status, giving us a way to test for the presence of input that had to be 'massaged'.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.