Question: 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
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(sizeofint)*10) In the example above integer pointer ptr is allocated a space of 10 blocks this is same as creating as array int ptri10] but here malloc function is used which allows the user to decide how much is required. memory char* string: string- malloc(sizeof(char) 15); has room for 15 characters Not even declaring the array is required, everything must be pointers. Implement following functions for the pre-lab. Description: Implement following functions for the prelab assignment. These wil be on the lab! int main0: Declares a new string (char pointer), then calls getString and prints the string with the size out and frees the malloced memory from the string. int getString(char*): This function takes in an uninitialized string, prompts the user for the size of the string (error checking to make sure size is between 1-20), and then prompts and scans in the actual string from the user and converts it to upper case. Returns the size of the string, and the string should be valid when the function terminates (hint: if the user enters in a string length less than the size but not empty, adjust the size. Using getSafeString from hw2 might be a good idea). int checkString(char*, int): Takes in the string and the size, and checks to see if the string is valid. Valid string should contain only alphabetical characters A' through Z, not including the null terminator. Return 1 if string is valid, otherwise O if string is invalid. (hint: try using the acsii values instead of checking for specific characters)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
