Click here to Skip to main content
14,608,056 members
Rate this:
Please Sign up or sign in to vote.
See more:
I can't find my bugs and its lacking the function for zigzag and I need help to complete this program. I have problem whit the places I have put a comment and the question is here.

You are required to copy the given main program together with the function headers exactly as they are with no change whatsoever and then implement the functions so that the program works correctly. Each function is described in detail in the comments given together with the function header. The output of a sample run of the given program is provided below for your reference. You can write additional helper functions that you can call from within your functions if you like; but you are not allowed to make any change to the given main program or the function headers. If you write any additional helper function, then it must be included in your submission. In order to help you avoid any typo and modification of any function signature, copy and paste the code provided in the starter code text file.
Restrictions
You are required to use C++ static or dynamic arrays of characters to store c-strings. You are NOT allowed to use any C++ string data type variable for any purpose. If you use a C++ string data type variable for any purpose in your program, you will automatically get zero mark. Moreover, you are allowed to add any include directive. You are not allowed to include string, cstdlib or math libraries. Also, you are not allowed to use any built-in functions of c-strings.

What I have tried:

charPointer copy = getCopy(s2);
 for (int i = 0; i < len1; i++)
 {
  removeCharAll(copy, s1[i]);

 }
 if (*copy == NULL) {
  return true;
 }
 else
  return false;

}

charPointer zigzagMerge(const charPointer& s1, const charPointer& s2)
{

 int len1 = cstrlen(s1);
 int len2 = cstrlen(s2);
 int newLen = len1 + len2;
 charPointer newcst = new char[len1 + len2];

 /*
 Creates and returns a new cstring by merging (combining) s1 and s2 in zigzag form.
 That is
 first character of the new cstring is the first character of s1
 second character of the new cstring is the first character of s2
 third character of the new cstring is the second character of s1
 fourth character of the new cstring is the second character of s2
 fifth character of the new cstring is the third character of s1
 sixth character of the new cstring 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 the new cstring
 For example, the zigzagMerge of "abc" and "defgh" will be "adbecfgh"
 */
}

charPointer getSubString(const charPointer& s, const int& startIndex, const int& len)
{
 /*
 returns a substring of s consisting of len characters starting from startIndex.
 If s has fewer characters starting from the startIndex upto its last character,
 then this function must return a substring consisting of only the available
 characters starting from the startIndex upto its last character in which case,
 the returned substring will have less than len characters.
 */
}

bool isSubString(const charPointer& s1, const charPointer& 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"
 Hint: Use the getSubString(const charPointer& s, const int& startIndex, const int& len) function above.
 */
}

int countWords(const charPointer& s)
{
 /*
 Given a cstring that contains some words separated by spaces, this function
 returns the number of words in the cstring.
 Here, a word means some characters with no any space in between.
 Example: If the cstring parameter is "What    a     nice           problem ".
 Then you see that there are FOUR words in this cstring, namely
 1. What      2. a       3. nice      4. problem
 Your function then must return 4

 For simplicity,
 1. Assume that there are no spaces at the beginning or at the end of the cstring
 2. But a word can be separated from another word by one or more spaces
 3. Assume there is no any tab in the cstring
 4. Assume there is no any punctuation mark in the cstring.
 */
}

int main()
{
 
 cout << "This program is designed to help you test your functions." << endl;
 srand(time(0));

 //Test cstrlen function
 cout << endl << "Testing cstrlen function";
 cout << endl << "------------------------" << endl;
 char s1[] = "irregular";
 cout << "The length of s1=\"" << s1 << "\" is " << cstrlen(s1) << endl;
 char emptyCstr[] = "";
 cout << "The length of \"\" is " << cstrlen(emptyCstr) << endl;

 //Test countChars functions
 cout << endl << "Testing countChars function";
 cout << endl << "---------------------------" << endl;
 char ch = 'r';
 int count = countChars(s1, ch);
 cout << "ch='" << ch << "' is found in s1=\"" << s1 << "\" " << count << " times." << endl;

 //Test findChar functions
 cout << endl << "Testing findChar function";
 cout << endl << "-------------------------" << endl;
 int a = 2, b = cstrlen(s1);
 int index = findChar(s1, ch, a, b);
 cout << "ch='" << ch << "' is found in s1=\"" << s1 << "\" in the index interval [" << a << ", " << b << ") at index " << index << endl;
 a = 3;
 index = findChar(s1, ch, a);
 cout << "ch='" << ch << "' is found in s1=\"" << s1 << "\" in the index interval [" << a << ", " << b << ") at index " << index << endl;
 b = 8;
 index = findChar(s1, ch, a, b);
Posted
Updated 30-Jul-20 20:09pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Assuming you have a function to compute the lenght of a string. Say s1 and s2 have length len1, len2. Then simply allocate (len1+len2+1) chars for the output variable s.
Finally write something like

for (charPointer p = s; *s1 || *s2;)
{
  if (*s1) 
  {
    *p = *s1;
     ++p;
     ++s1;
  }
  if (*s2)
  {
    *p = *s2;
    ++p;
    ++s2;
  }
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100