Please write the code in C programming language You may not
Please write the code in C programming language.
You may not call functions in string.h but you can use other code in the Standard C Library.
Functions to Include in the Library
Implement each of the following functions. Be sure that any string that you create or modify is in fact a string, i.e., an array of char terminated with the null character, \'\\0\'.
1. void take_last(char *s, int n)
Modifies s so that it consists of only its last n characters. If n is the length of s, the original string is unmodified. For example if we call take_last(\"Brubeck\" 5), when the function finishes, the original string becomes \"ubeck\"
2. dedup(char *s)
returns a new string based on s, but without any duplicate characters. For example, if s is the string, \"There\'s always money in the banana stand.\", the function returns the string \"Ther\'s alwymonitbd.\". It is up to the caller to free the memory allocated by the function.
3.pad(char *s, int d)
returns a new string consisting of all of the letters of s, but padded with spaces at the end so that the total length of the returned string is an even multiple of d. If the length of s is already an even multiple of d, the function returns a copy of s. The function returns NULL on failure or if s is NULL. Otherwise, it returns the new string. It is up to the caller to free any memory allocated by the function.
Solution
/*
*
* Below Function declerations. These functions can be moved to any header file and
* add the header file her like:
* #include \"blah..blah.h\"
*
*/
void take_last(char *s, int n);
char *dedup(char *s);
char *pad(char *s, int d);
/*
* Function: take_last
* Parameters: char* s, n
* Return: None
*/
void take_last(char *s, int n) {
if (!s) // if string is empty, simply return.
return;
char *t = s;
int i, j, len = 0;
while (*t != \'\\0\') { // find out the string length
t++;
len++;
}
if (len < n) // if string length less than n, then no need to truncate the string.
return;
for (i = 0, j = n; i < n; i++, j--)
s[i] = s[len - j];
s[i] = \'\\0\';
}
/*
* Function: dedup
* Parameters: char* s
* Return: char*
*/
char *dedup(char *s) {
if (!s) // If string is empty, return NULL
return NULL;
char *t = s;
char used[256];
int i, j, len = 0;
// Get length
while (*t != \'\\0\') {
t++;
len++;
}
t -= len;
if (len < 2)
return s;
// Array of used chars
for (i = 0; i < 256; i++)
used[i] = 0;
char *new = (char *)malloc(sizeof(char) * len);
if (new == NULL)
return NULL;
for (i = 0, j = 0; i < len; i++) {
if (!used[s[i]]) { // Check if character has been used
new[j++] = s[i];
used[s[i]] = !0;
}
}
new[j] = \'\\0\';
return new;
}
/*
* Function: pad
* Parameters: char* s, d
* Return: char*
*/
char *pad(char *s, int d) {
if (!s) // if string is empty, return NULL
return NULL;
char *t = s;
int i, len = 0;
// Get length
while (*t != \'\\0\') {
t++;
len++;
}
t -= len;
int a = (int)ceil((double)len/d);
if ((a * d) % 2 == 1)
a++;
d *= a;
char *new = (char *)malloc(sizeof(char) * (d + 1));
if (new == NULL) // if memory is not allocated, then return NULL
return NULL;
for (i = 0; i < d; i++) {
if (i < len)
new[i] = s[i];
else
new[i] = \' \';
}
new[i] = \'\\0\';
return new;
}


