Question
8.7Exercises For the following set of exercises, create a header file called lab3.h. Each exercise will ask you to create a specific function in your
8.7Exercises
For the following set of exercises, create a header file called lab3.h. Each exercise will ask you to create a specific function in your header file.
String Regrouping [10 points]
Given a string of alphanumeric characters, create a function in your header file with the following signature:
std::string regroup ( std::string )
which returns the string with all the letters of the alphabet grouped to the left and the numerical characters on the right.
Example: "H2SO4" transforms to "HSO24", "COVID19" stays the same.
Count Upper Case Letters [10 points]
Write a function in your header file that counts the number of upper case letters in a given string. It should have the following signature:
int count_upper( std::string )
Examples: If the input string is "Hello World", the result is 2, because there are 2 upper case characters in it.
Invert String Case [10 points]
Write a function in your header file, with the following signature:
std::string transform_case( std::string )
which inverts the case of the input string and returns the result. Inverting the case means, transforming uppercase letters into lowercase letters, and vice-versa, with the exception of the letter 'E', which should always be in uppercase, and the letter 'A', which should always be unmodified.
Example: "Hello World" transforms to "hELLO wORLD", and "Alphabet" transforms to "ALPHaBET", and "Earth" transforms to "EaRTH"
String Rotations [10 points]
A string rotation is defined as taking the first character and placing it in the last position or taking the last character and placing it in the first position (left and right rotations respectively). The rotation distance between two strings is the minimum number of rotations needed to be performed on one string so that it is equal to the other. If the strings can not be transformed into one another, then their distance is -1.
Examples: "ABCD", and "BCDA" have a distance of 1, because we needed to rotate "ABCD" once. The strings "ABCD", "BCAD" have a distance of -1 because they can not be rotated into each other.
Write a function in your lab3.h header file with the following signature:
int rotation_distance( std::string a, std::string b)
which returns the rotation distance of the given strings.
String Compression [10 points]
A simple compression algorithm is to replace repeated sequences of a character with the character written once followed by the number of times it repeats.
For example the string "AAACTGG" can be compressed as "A3CTG2". Since there are 3 As at the beginning, we have A3, then there is only 1 C, so we just write "C", not "C1" because that would take more space. Same idea behind "T", and finally, we have "GG" which is the same as "G2".
Write a function in your header file with the following signature:
std::string compress ( std::string )
which takes in a string and returns its compression.
Reflectional Symmetry of Strings [20 points]
A string is said to be symmetric if its left and right halves are mirror images of each other. If the string has an odd length, the middle character is cut in half. For example, the character "O" is symmetric because if we cut in in half across the middle, we get two halves that are mirror images of each other. The character "A" is also symmetric because curring the letter A in to a left and a right half, the two halves are mirror images of each other. The characters "G", and "E" are not symmetric. This is not an exhaustive list of all letters of the alphabet but you can see which upper case letters are symmetric and which are not.
When it comes to strings of length 2, you again cut down the middle and check if the halves are mirror images. So the string "AA" is symmetric, the string "AI" is not. The following strings are all considered symmetric: "1I", "2S", "5S", "3E", "9P", "JL", even though the symmetry is not perfect.
Note: This exercise is concerned with upper case letters, and numbers only. No need to include lower case letters, and/or other symbols.
More examples:
-
"HELLO" - not symmetric
-
"JOL" - symmetric
-
"E83" - symmetric
In your lab3.h file, include a function with the following signature:
bool symmetric ( std::string )
which returns true if the given string is symmetric and false otherwise. You may use as many helper functions as you want.
Unit Tests [30 points]
Create a file called lab3test.cpp and write unit tests for each of the functions above. The correctness of your unit tests will be assessed.
9Functions
Coming soon
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started