rana hushiar Ответов: 1

Как написать код для zigzag со строкой C++


Я не могу найти свои ошибки и не хватает функции для zigzag, и мне нужна помощь, чтобы завершить эту программу. У меня есть проблема с теми местами, где я поставил комментарий, и вопрос здесь.

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.
Ограничения
Вы должны использовать статические или динамические массивы символов C++ для хранения c-строк. Вам не разрешается использовать любую переменную типа данных string C++ для каких-либо целей. Если вы используете переменную строкового типа данных C++ для какой - либо цели в вашей программе, вы автоматически получите нулевую отметку. Кроме того, вы можете добавить любую директиву include. Вам не разрешается включать библиотеки string, cstdlib или math. Кроме того, вам не разрешается использовать какие-либо встроенные функции c-strings.

Что я уже пробовал:

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);

1 Ответов

Рейтинг:
0

CPallini

Предположим, что у вас есть функция для вычисления длины строки. Скажи s1 и s2 иметь длину len1, len2 Затем просто выделите (len1+len2+1) charдля выходной переменной s.
Наконец напишите что-нибудь вроде

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