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.
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 :
Hey everyone, I need help making a function with this directions with C++ Language. Can you...
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 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...
implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...
**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 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 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...
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...
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. //...
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...
****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;...