Starting from:
$35

$29

COMP 2012H Assignment 20A Simple Text Processor Solution

The objective of this assignment is to implement a few functionalities of a text processor.0A text processor allows users to manipulate a string of characters.0These manipulations provide innumerable options with which to personalise a document.0For instance one can:

Change the alignment of the text so as to right align, left align, or justify (right and0left align) it.

Perform global replacement of a certain string in the text with another string.

Keep track of the number of words and characters in the text.

Change the case (upper or lower) of a certain piece of text.

Print the text in various formats.

Count the number of appearances of a certain string in a text.

In this assignment, you will be required to:

Implement tailored versions of the mechanisms outlined above.

Implement a simple Caeser Cipher

Print a character array in an argot called Pig Latin

End of Introduction

Description

Please read the FAQ section regularly, and do check it0one day before the deadline to make sure you don't miss any clarification,0even if you have already submitted your work by then. You can also raise questions0on Piazza and remember to use the "pa2" tag.

Code structure

The skeleton code structure is as follows:

PA20

└── main.cpp0

Your task for this PA is to implement 10 functions in main.cpp.

Caeser Cipher

The Caeser Cipher is a simple0shift cipher that modifies alphabetical words0in order to produce a text whose meaning has been obfuscated. Ciphers are used to encrypt0text to make it unreadable.0A Shift Cipher achieves this by taking a number shift and shifting every alphabetical character of the text by the alphabet that is shift modulo 260positions0down the alphabet. For instance, with a right shift of 5, 'A' would become 'F', 'G' would0become 'L', and 'X' would become 'C'. Note that uppercases and lowercases are maintained.

https://course.cse.ust.hk/comp2012h/assignments/PA2/Description/ 2/9

11/25/22, 9:47 PM COMP 2012H Assignment 2: A Simple Text Processor

A simple Shift Cipher that rotates left by 3 (or, alternatively, right by -3).0

Source: Link

If shift is 5:

"Object-Oriented Programming" would be "Tgojhy-Twnjsyji Uwtlwfrrnsl"

"The quick brown fox jumps over the lazy dog" would be "Ymj vznhp gwtbs ktc ozrux tajw ymj qfed itl"

Pig Latin

Pig Latin is a0Language Game that modifies English words0to produce a "cryptic" rendition that makes it hard to follow and understand. This is often0used by children who want to converse with0each other without letting adults understand what they are saying.0Every word is individually converted from English to Pig Latin using the following0conversion rules:

If the first letter is a vowel ('a', 'e', 'i',0'o', 'u') then simply append "yay" to it. For example:

"ointment" -> "ointmentyay"

If the first letter is not a vowel then shift it to the end until the first letter is a0vowel and then append "ay" to it. For example:

"consume" -> "onsumec" -> "onsumecay"

"cheap" -> "heapc" -> "eapch" -> "eapchay"

"warm-up" -> "arm-upw" -> "arm-upway"

Input Assumptions

For the string (char array) that you are operating on, you can assume that:

It is correctly terminated with a '\0' character.

The string will not be longer than MAX_STRLEN. All the chracters including0'\0' can be stored inside an array char str[MAX_STRLEN].0This one will still hold even after any operations (functions) you need to implement.

The string will only contain these characters before the '\0' character:

'a' - 'z' (lowercase letters)

'A' - 'Z' (uppercase letters)

'-' (hyphen)

',' (comma)

'.' (fullstop)

' ' (whitespace)

Important Requirements

There are a few things you CANNOT do. Failure to observe these rules would potentially0result in ZERO mark for your assignment. Please carefully check your code before you submit0it.

This assignment is all about string operation exercises, so you are NOT allowed to include any additional libraries for example cstring and0string.h.

You are NOT allowed to modify any of the given function prototypes including the return types, function names, and parameter lists.

You are NOT allowed to define any additional global variable. You can only use those provided in the skeleton codes.

End of Description

https://course.cse.ust.hk/comp2012h/assignments/PA2/Description/ 3/9

11/25/22, 9:47 PM COMP 2012H Assignment 2: A Simple Text Processor

Tasks

This section describes the 10 functions that you will need to implement.

int countCharacters(const char str[]);

Description - Returns the number of characters in str.0

For example, if:

str is "Hello world.",

Then the function would be return 12. (10 letters + 1 whitespace + 1 fullstop)0

Parameters

str - string of text.

Return value - Number of characters in str until the0null-terminator (\0 itself excluded).

int countWords(const char str[]);

Description - Returns the number of words in str. Read the0notes below for the definition of words.0

Parameters

str - string of text.

Return value - Number of words in str until the0null-terminator \0.0

Notes

Words will only contain letters and hyphens.

All consecutive letters or hyphen together forms a word. That means between each two words there are at least one non-word character. For example:0

The string "Here is the well-designed program." contains 5 words.

There will be some "strange" words under this definition, e.g. "-", "-abc", "abc-",0it's fine.

We still count them.

void swapString(char str[], const char target[], const char to[]);

Description - Replaces every instance of the characters in0target in str with the characters in

to.0

For example, if:

str is "I have a course in the morning".

target is "course".

to is "zoom meeting".

Then str would be updated to be0"I have a zoom meeting in the morning".0

If:

str is "I would like an ice cream".

target is "ice ".

to is "ice-".

Then str would be updated to be "I would like an ice-cream.".0

Note that each characrer can only be matched at most once, so if:

t is " "

https://course.cse.ust.hk/comp2012h/assignments/PA2/Description/ 4/9

11/25/22, 9:47 PM COMP 2012H Assignment 2: A Simple Text Processor

str is "ssss".

target is "sss".

to is "a".

Then str would be updated to be "as" but not "aa".0

Parameters

str - string of text.

target - the target substring to be replaced.

to - the new substring to replace target.

Notes

The replacement could change the total length of str, make sure that0the null-terminator is placed correctly after the replacement.

void encryptText(char str[], int shift);

Description - Encrypts the text in str using a Shift Cipher by0shift amount shift to the right.

Note that shift can be negative to introduce a left shit. You may want to read0the intro on

Caeser Cipher first.0

For example, if:

str is "She sells seashells on the seashore.", and

shift is 15

Then str would be updated to be0"Hwt htaah htphwtaah dc iwt htphwdgt.".0

Parameters

str - string of text.

shift - the amount to right shift the alphabet0characters by.

Note

Your codes should be able to handle inputs like shift == 53 which is0simply equivalent to

shift == 1.

Your codes should be able to handle negative inputs like shift == -770which is simply equivalent to shift == 1.

'a' <= c && c <= 'z' will tell you whether character c is0a lowercase letter.

int countNumOccurences(const char str[], const char target[]);

Description - Counts the number of occurences of target in0str.0

For example, if:

str is "Why have a ballroom with no balls.".

if target is "balls", then the function would return 1.

if target is "ball", then the function would return 2.

if target is " ball", then the function would return 2.

if target is "ball ", then the function would return 0.

Note that each character can be matched at most once, so if:

str is "ssss".

target is "ss"

Then the result should be 2 but not 3.0

Parameters

str - string of text.

target - the substring to be counted.

Return value - the number of occurences of target in0str.

void convertIntoLines(const char str[], char lines[MAX_LINES][NUM_CHARS_PER_LINE]);

Description - Converts str into lines which contain at most0NUM_CHARS_PER_LINE0characters (including the null-terminator, that means you actually can only have0NUM_CHARS_PER_LINE - 1 meaningful characters per line) and save the lines with0lines.0

https://course.cse.ust.hk/comp2012h/assignments/PA2/Description/ 5/9

11/25/22, 9:47 PM COMP 2012H Assignment 2: A Simple Text Processor

Parameters

str - string of text.

lines - a 2D char array, the container of the results.

Rules for Conversion

Insert as many characters to a line as possible, but remember to leave one position0for '\0'.

If there is not enough space for the last word in current line, then terminate the0line and start a new line. The word will be put to the new line.

If a line is full, then terminate the line and start a new line.

We will not insert any whitespaces to the beginning of a line. They will be skipped. Discard all the tailing whitespaces after lines are split. That0means, in each line of the final results, the second last character is not ' ' (the last character is '\0').

If there are unused lines in the end, make sure to put a '\0' to the0first position of them.

For example, suppose NUM_CHARS_PER_LINE = 10. Then if:

str is0"Mary had a little lamb, little lamb, little lamb".

Then the corresponding lines would be:

lines[0] = "Mary had\0"

lines[1] = "a little\0"

lines[2] = "lamb,\0"

lines[3] = "little\0"

lines[4] = "lamb,\0"

lines[5] = "little\0"

lines[6] = "lamb\0"

In this function, you may assume that the results will not exceed0MAX_LINES0lines. Also there won't be words longer than NUM_CHARS_PER_LINE - 10characters, otherwise it's impossible to solve.

void printLeftJustified(const char str[]);

Description - Prints str in a left-aligned format. This can be0done simply by calling the convertIntoLines function you just implemented and0then print the lines one by one.0

Parameters

str - string of text.

Notes

Only print those used lines, do not print those unused lines.

Use the example in last task again, suppose NUM_CHARS_PER_LINE = 10:

if code is0"Mary had a little lamb, little lamb, little lamb".

Then the function will output:

Mary had0

• little0

lamb,0

little0

lamb,0

little0

lamb0

void printRightJustified(const char str[]);

Description - Prints str in right-aligned format. It means0that you will insert blank spaces to the beginning of each line so that the total character0count of each line is exactly NUM_CHARS_PER_LINE - 1 ('\0'0excluded as it will not be part of the output).0

Parameters

str - string of text.

https://course.cse.ust.hk/comp2012h/assignments/PA2/Description/ 6/9

11/25/22, 9:47 PM COMP 2012H Assignment 2: A Simple Text Processor

Notes

Again, make use of your implemented convertIntoLines() function.

Only print those used lines, do not print those unused lines.

The example again, suppose NUM_CHARS_PER_LINE = 10:

if code is0"Mary had a little lamb, little lamb, little lamb".

Then the function will output:

Mary had0

• little0

lamb,0

little0

lamb,0

little0

lamb0

void printJustified(const char str[]);

Description - Prints str in right-left-aligned format. It0means that you will insert blank spaces to the0middle of each line (except the last line) so that the total character count of each line (except the last line) is exactly NUM_CHARS_PER_LINE - 1 ('\0'0excluded as it will not be part of the output).0

Parameters

str - string of text.

Notes

Again, make use of your implemented convertIntoLines() function.

You first compute how many white spaces you need to insert into a line.

Then, find all pieces of consecutive white spaces in the original line.

Starting from the first piece of consecutive white spaces, add one more white space0to it, and then add to the next piece. If after adding to the last piece, you still0have extra white spaces to be added, then start from the first piece again.

Consider such a line, we use black color to represent non-space characters, and0white color to represent space characters, the number represents number of0characters.0

The above line contains 20 characters, let's say NUM_CHARS_PER_LINE is025, then we need to insert 4 extra spaces to this line. There are 3 pieces of0consecutive white spaces in this line, so we will add one more space to each of0them. Then there is still one more space to add, we start from the first piece again0and add that final white space to it. The final printed line will look like this:0

You may assume that there is at least one white space in each line (except the last0line).

The last line will still be printed in left-aligned format.

Please try the demo program to see how it works.

void convertStrToPigLatin(char str[]);

Description - Converts the words in str to Pig Latin. You may0want to read the intro on Pig Latin first.0

Parameters

str - string of text.

https://course.cse.ust.hk/comp2012h/assignments/PA2/Description/ 7/9

11/25/22, 9:47 PM COMP 2012H Assignment 2: A Simple Text Processor

Notes

You don't need to worry about letter cases in this task. We will eventually convert0all letters in your results to lower cases in the skeleton codes.

You can assume that all words will have at least one vowel letter for this task.

An example:

if str is "I would like to have an ice-cream."

then str would be updated to0"iyay ouldway ikelay otay avehay anyay ice-creamyay."

Again, the letter case doesn't matter, skeleton codes will take care of it.

This operation could change the total length of str, make sure that0the null-terminator is placed correctly after the replacement.

End of Tasks

Resources & Sample I/O

Skeleton code: PA2_Skeleton.zip

Demo programs (last update on 9/9 18:38):0

Windows /0Linux /0macOS x86 /0macOS arm0

(If you find the demo program doesn't behave as you expect, please first download and try the lastest version, if the problem is still there you can let me know. Thanks!) Sample program outputs:0Sample Outputs

If you encounter problems running the MacOS demo programs, you can try look for a Windows PC0on campus or use0the Virtual Barn0from anywhere to run the Windows demo program.

End of Resources & Sample I/O

More products