Question: 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
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 as array
int ptr[10] but here malloc function is used which allows the user to decide how much memory is
required.
Another example
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 will be on the lab!
int main():
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 0 if string is invalid. (hint: try using the acsii values
instead of checking for specific characters)
Sample output:
Enter the size of the string:
109
Please enter again:
-1
Please enter again:
7
Please enter the string:
0
Please enter a valid string:
stringstringstring
The string entered is longer than the allowed size
Please enter a valid string:
89994
Please enter a valid string:
5tring5
Please enter a valid string:
String
You entered: STRING which is size 6
./a.out
Enter the size of the string:
0
Please enter again:
18
Please enter the string:
^I$@OI$
Please enter a valid string:
baseball bat
Please enter a valid string:
baseball
You entered: BASEBALL which is size 8
Note:
1. Dont use any global variables, though a global constant for the maxsize of string (20) is ok.
2.
Use only pointer notation and pointer arithmetic to implement the assignment.
3. Use the relevant library function, especially from ctype.h
Library function youll need to free the string at the end:
https://www.tutorialspoint.com/c_standard_library/c_function_free.htm
Remember that the string is passed by reference to the function: changing its contents in the function
changes it in main as well, including mallocing space for it.
Ascii table:
http://www.asciitable.com/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
