Question

Hey everyone, I need help making a function with this directions with C++ Language.

Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet.

Thanks.

Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the array passed in, then copy those strings, one after the other, into the newly allocated space. The original strings must remain unchanged. The function should return the pointer to the newly allocated space or a NULL pointer if no space was allocated The code below shows an example of how this function might be called. struct words int numberofWords; char theWords [ARRAY_SIZE] [FILENAME MAX]; typedef struct words Words; // alloccat prototype (declaration) goes here: // sample main shows how allocCat might be called int main (void) Words mywords = { ARRAY-SIZE, { stuff, and, nonsense } }; char* ful!String = NULL; int returnValue EXIT SUCCESS; fullstring = alloccat(&myWords ); //calls the function you write below if (ful ! String== NULL) // get out if memory not allocated fprintf (stderr, Could not allocate memory\n returnValue = EXIT FAILURE ; else puts (fullstring); free (fullString); // prints out the new string // releases memory getch ); return returnValue this sample code above would print out stuff and nonsense ! WARNING: The code given above is an example of how the function might be used. Do not try to use the sample code above in the function definition! The function has no ilo! Hint: This function will need to calculate the length of the strings passed in. It needs to be able to work with ANY STRINGS, not just those shown in the example char* allocCat(

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The solution for the given program is given below with the screenshot of output.

=========================================================

I have not used iostream and fstream as you asked in question. I kept the code simple and made

it similar to c, so it could be easily understood.

I have added comments to program, if there is anything else do let know in comments.

=========================================================

=================== CODE TO COPY ===========================

#include <cstdio>
#include <cstdlib>

// structure for words struct
struct words{
int word_count;
char theWords[1024][80];
};

// typedef for struct words
typedef struct words Words;

// function prototype
char * allocCat( const Words * w);

int main() {

// here we are adding 4 words to onu struct
Words words_array = { 4, { "writting" , "a", "c++", "program" } };

// passing the address of struct to function
char *new_sentence = allocCat( &words_array );

// if memory Could not be allocated we print msg and return
if ( new_sentence == NULL )
{
fprintf(stderr, "Could not allocate memory for words\n");
return 1;
}

fprintf(stdout, "New string is %s\n", new_sentence );
free( new_sentence );
return 0;

}

char * allocCat( const Words * w){
  
// calculating how many elements are there in the array
int array_length = w->word_count;
  
//printf("The number of elements in array %d ", array_length);
  
int element_length = sizeof(w->theWords[0]);
//printf("The length of elements in array %d ", element_length);
  
char *p = ( char *)malloc( array_length * element_length );
char *temp = p;
  
// if we cannot allocate space return null
if ( p == NULL )
return NULL;
  
// add all elements togather and return this pointer
for (int i = 0 ; i < array_length; i++ )
{
int j = 0;
while ( *temp++ = w->theWords[i][j++] );
temp--;
}
  
  
return p;
}

=========================================================

output :

gcc version 4.6.3 New string is writtingac++program 2

Add a comment
Know the answer?
Add Answer to:
Hey everyone, I need help making a function with this directions with C++ Language. Can you...
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
  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • 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...

  • **C** Write a C function that inputs a pointer to a string and a pointer to...

    **C** Write a C function that inputs a pointer to a string and a pointer to a buffer. The function then scans through the string removing leading and trailing whitespace and replacing any run of whitespace within the string with a single space. Your function should follow this prototype: void tighten(char *oldstring, char* newstring, int length); The first argument is a pointer to a null-terminated string sitting somewhere in memory. The second argument is a pointer to the first char...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

  • 2. (50 marks) A string in C++ is simply an array of characters with the null...

    2. (50 marks) A string in C++ is simply an array of characters with the null character(\0) used to mark the end of the string. C++ provides a set of string handling function in <string.h> as well as I/O functions in <iostream>. With the addition of the STL (Standard Template Library), C++ now provides a string class. But for this assignment, you are to develop your own string class. This will give you a chance to develop and work with...

  • 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. //...

  • C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and...

    C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and x,y pair object) 24. Write a function that searches a numeric array for a specified value. The function should return the subscript of the element containing the value if it is found in the array. If the value is not found, the function should throw an exception. 25. Write a function that dynamically allocates a block of memory and returns a char pointer to...

  • Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like...

    Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like ones in attachment ab Ass Part 1 sing Static Arra Do this part on your own. Write a program named lab11 a.c containing the following function: preconditions arc is terminated by dest is big enough hold arc postconditions dest contains src and is terminated by "10" void my strcpy (char dest const char srclj) This function will take two character arrays as parameters. The...

  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

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