Question

I am failing one of the tests cases for the trimArray function. The test that my...

I am failing one of the tests cases for the trimArray function. The test that my function is failing is testing that when you get a toTrim array of  " ", it should return "" (an array of no characters). How do I incorporate that functionality into the function and where will it go in the C++ code?

Here's my function:

// The function trimArray() is given a pointer to a character array that
// is NULL terminated called toTrim. This function is to remove any space characters
// that exist between the last non-space character and the NULL character in the toTrim array.
// Example, the following character string "This text " should be trimmed
// (or altered) to be "This text". The trimmed array is put in the array
// pointed to by the newArray pointer. Assume that the array pointed to by newArray
// is the same length as the toTrim array. If there are no spaces to trim a copy of
// the toTrim array should be made to the newArray.If the toTrim array has no characters
// then the newArray should have no cahracters(a null termination only).
void trimArray(const char *toTrim, char *newArray)
{
   //Initialization of the length variable to 0
   int length = 0;
   //Integer Index Variable (i)
   int i;
   //Integer Index Variable (j), initialized to 0
   int j = 0;

   //For loop gonig through the array toTrim to find the NULL character aka the length of the array
   for (i = 0; toTrim[i] != '\0'; i++)
   {
       //Incrementation of length + 1 each time a character other than the NULL character is found
       length++;
   }
  
   //For loop gonig through the array toTrim to find all of the characters that are not spaces
   //Starts at the last index of the array, and goes down by one each time
   //As long as i >= 0
   for (i = length - 1; i >= 0; i--)
   {
       //If condition setting all the non-space elements of the array equal to the values of another index
       //No space = character
       if (toTrim[i] != ' ')
       {
           //Set the current index to the j variable
           j = i;
           break;
       }
   }
  
   //For loop going through the arrays newArray and toTrim at index i setting the values equal until j
   for (i = 0; i <= j; i++)
   {
       //All characters from the toTrim Array are put into the newArray
       newArray[i] = toTrim[i];
   }
   //Adding a NULL character to the last index of the newArray
   newArray[j + 1] = '\0';

}

Any help with an explanation would be greatly appreciated.

Thank you,

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

void trimArray(const char *toTrim, char *newArray)
{
   //Initialization of the length variable to 0
   int length = 0;
   //Integer Index Variable (i)
   int i;
   //Integer Index Variable (j), initialized to 0
   int j = 0;

   //For loop gonig through the array toTrim to find the NULL character aka the length of the array
   for (i = 0; toTrim[i] != '\0'; i++)
   {
       //Incrementation of length + 1 each time a character other than the NULL character is found
       length++;
   }

   //For loop gonig through the array toTrim to find all of the characters that are not spaces
   //Starts at the last index of the array, and goes down by one each time
   //As long as i >= 0
   for (i = length - 1; i >= 0; i--)
   {
       //If condition setting all the non-space elements of the array equal to the values of another index
       //No space = character
       if (toTrim[i] != ' ')
       {
           //Set the current index to the j variable
           j = i;
           break;
       }
   }

   // count starting spaces, and do not include them
   int spaces = 0;

   //For loop going through the arrays newArray and toTrim at index i setting the values equal until j
   for (i = 0; i <= j; i++)
   {
    if(spaces == 0 && toTrim[i] == ' ') {
        spaces++;
    } else {
            newArray[i - spaces] = toTrim[i];
    }
       //All characters from the toTrim Array are put into the newArray
   }

   //Adding a NULL character to the last index of the newArray
   newArray[j + 1 - spaces] = '\0';

}

using namespace std;

int main() {
    char dest[100];
    trimArray(" hi ", dest);
    cout << "*" << dest << "*" << endl;
    trimArray(" hi", dest);
    cout << "*" << dest << "*" << endl;
    trimArray("hi ", dest);
    cout << "*" << dest << "*" << endl;
    trimArray(" ", dest);
    cout << "*" << dest << "*" << endl;
}




Hi. please find the answer above.. i have given comments so that it is very easy for you to understand the flow. In case of any doubts, please ask in comments. If the answer helps you, please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
I am failing one of the tests cases for the trimArray function. The test that my...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can you help me make these two methods listed below work? I have code written but...

    Can you help me make these two methods listed below work? I have code written but it is not working. I would appreciate any advice or help. Function "isAPalidrome" accepts an array of characters and returns an integer. You must use pointer operations only, no arrays. This function should return 1 (true) if the parameter 'string' is a palindrome, or 0 (false) if 'string' is not a palindrome. A palindrome is a sequence of characters which when reversed, is the...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • Based on the preconditions, did i coded this function correctly in c++? /* Creates an array...

    Based on the preconditions, did i coded this function correctly in c++? /* Creates an array of HuffmanNodes (more specifically, pointers to HuffmanNodes) based on the given character-frequency pairs. characters: an array of char frequencies: an array of int length: the length of characters and frequencies arrays returns: an array of HuffmanNode* Note: For 0 <= i < length, the frequency of the character characters[i] is found at frequencies[i]. */ HuffmanNode **genHuffmanNodes(char *characters, int *frequencies, int length) { // TODO...

  • Please help ASAP Question 3 (10 points): Using pointer notation, complete the C++ function below using...

    Please help ASAP Question 3 (10 points): Using pointer notation, complete the C++ function below using pointcr notations The function takes N characters in character array A and return true if the characters are consecutive and fahe otherwise. bool consecutiveChars(char A, int N) char first; // Pointer to the first character in A char last; // Pointer to the last character in A Your code here.. #include <iostream> #include <cctype> using namespace std; bool consecutiveChars(char* A, int N): nt main0...

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • How to replace elements in a 2D array? Okay so for my program, I am trying...

    How to replace elements in a 2D array? Okay so for my program, I am trying to create a fish tank. My program is to generate 4 different FISH: ><))'> in a tank of tilde (~) characters. The tank is a 2D array of 8 rows and 32 columns so it would look like ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for one of the eight rows (before I generate the random positions of the fish in the rank). Then one row could look like ~~~~~~~~~~><))'>~~~~~~~~~~~~~~~~...

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

  • Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2...

    Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I need some help because if the arrays for string4a and string4b have different sizes. Also if I put different sizes in string3a and string4b it will say that those arrays for 3a and 3b are corrupted. But for the assigment I need to put arrays of different sizes so that they can do their work...

  • Malloc function For the prelab assignment and the lab next week use malloc function to allocate...

    Malloc function For the prelab assignment and the lab next week use malloc function to allocate space (to store the string) instead of creating fixed size character array. malloc function allows user to allocate memory (instead of compiler doing it by default) and this gives more control to the user and efficient allocation of the memory space. Example int *ptr ptr=malloc(sizeof(int)*10); In the example above integer pointer ptr is allocated a space of 10 blocks this is same as creating...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT