instructions
Due Date: Wednesday 4th April 2018 at 11:55PM In this assignment, we will work on c-strings. In order to store c-strings, we will use C++ static or dynamic character arrays. You are not allowed to use the C++ string data type in this assignment. Read the restriction section below. Consider the main program together with function headers given below: #include using namespace std; int stringLength(const char* s) { //returns the number of printable characters in s } int countChars(const char* s, const char ch) { //returns the number of times the character ch is found in s } int findChar(const char* s, const char ch, const int startIndex, const int lastIndex) { /* returns the first index where the character ch is found in s starting from startIndex (inclusive) upto lastIndex (exclusive) If ch is not found in s in the interval, it returns -1 This function must first validate both the startIndex and lastIndex. That is, if lastIndex > stringLength(s) or startIndex < 0 it must return -1 */ } void rotateString(char* s, const int r) { /* Rotate the characters of s by r units If r > 0, rotate the characters of s to the left If r < 0, rotate the characters of s to the right Please note the value of r can be any integer even larger than the length of s */ } void append(char*& s, const char ch) { /* Appends the character ch to the c-string s. That is ch is added to the end of s The parameter s is assumed to be a dynamic array (NOT a static one) */ } (Ph.D.) Page 1 void append(char*& s1, const char* s2) { /* Appends all the characters of s2 to s1 The parameter s1 is assumed to be a dynamic array (NOT a static one) */ } void removeAll(char*& s, const char ch) { /* remove all the occurences of the character ch from the c-string s The parameter s is assumed to be a dynamic array (NOT a static one) */ } char* zigzagMerge(const char *s1, const char* s2) { /* create and return a new c-string by merging (putting in one) s1 and s2 in zigzag form. That is first character of the new c-string is the first character of s1 second character of the new c-string is the first character of s2 third character of the new c-string is the second character of s1 fourth character of the new c-string is the second character of s2 fifth character of the new c-string is the third character of s1 sixth character of the new c-string is the third character of s2 etc When either s1 or s2 reaches to its end, the remaining characters of the other are appended to new c-string Example, zigzagMerge of “abc” and “defgh” will be “adbecfgh” */ } bool isAnagram(const char *s1, const char* s2) { /* returns true if s1 and s2 contain same distinct characters apearing same number of times in both s1 and s2 otherwise returns false That is, this function returns true if s1 and s2 are permutations (re-arrangements) of same characters */ } bool isSubstring(const char *s1, const char* s2) { /* returns true is s1 is a substring of s2 otherwise returns false Definition: s1 is a substring of s2 if s1 is found in s2. That is all characters of s1 are found TOGETHER in s2 in the SAME ORDER as they appear in s1 Example “set” is a substring of “massachussettes” But “ets” is not substring of “massachussettes” */ } int countWords(const char* s) { /* Given a c-string that contains some words separated by spaces, return the number of words in the c-string. (Ph.D.) Page 2 In this case, a word means some characters with no space in between. Example: If the c-string parameter is “What a nice Then you see that there are FOUR words in this c-string, namely 1. What 2. a 3. nice 4. problem Your function then must return 4 problem”. For simplicity, 1. Assume that there are no spaces at the beginning or at the end of the c-string 2. But a word may be separated from another word by more than one space 2. Assume the parameter does not contain any punctuation marks such as full stop or comma. */ } int main() { /* This main program is designed to test the functions you need to implement. You should NOT remove any line of code from this main program. But you may add more test code in the main program if you like. */ //Test stringLength function cout << endl; char s1[] = “massachussettes”; cout << s1 << ” has ” << stringLength(s1) << ” characters” << endl; //Test countChars function cout << endl; char ch = ‘s’; int c = countChars(s1, ch); cout << ch << ” appears ” << c << ” times in ” << s1 << endl; //Test findChar function cout << endl; int index = findChar(s1, ch, 10, 14); if (index == -1) cout << ch << ” is not found in ” << s1 << ” between indexes [10, 14)” << endl; else cout << ch << ” is found at index ” << index << ” in ” << s1 << ” between indexes [10, 14)” << endl; //Test rotateString function cout << endl; char temp1[] = “massachussettes”; rotateString(temp1, 2); cout << s1 << ” rotated 2 units to the left becomes ” << temp1 << endl; char temp2[] = “massachussettes”; rotateString(temp2, -19); cout << s1 << ” rotated 19 units to the right becomes ” << temp2 << endl; //Test append function (appending a character to c-string) cout << endl; char* s2 = new char[1]; s2[0] = ‘