Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write the following code in C programming language. You may not call func

ID: 3714240 • Letter: P

Question

Please write the following 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, ''.

Additionally, you should write a driver which tests each of these functions on real data.

[2 points] int all_letters(char *s)

Returns 1 if all of the characters in the string are either upper- or lower-case letters of the alphabet. It returns 0 otherwise

[2 points] num_in_range(char *s1, char b, char t)

returns the number of characters c in s1 such that b<=c<=t

[4 points] diff(char *s1, char *s2)

returns the number of positions in which s1 and s2 differ, i.e., it returns the number of changes that would need to be made in order to transform s1 into s2, where a change could be a character substitution, an insertion, or a deletion.

[2 points] void shorten(char *s, int new_len)

Shortens the string s to new_len. If the original length of s is less than or equal to new_len, s is unchanged

[2 points] int len_diff(char *s1, char *s2)

Returns the length of s1 - the length of s2

[2 points] void rm_left_space(char *s)

removes whitespace characters from the beginning of s

[2 points] void rm_right_space(char *s)

removes whitespace characters from the end of s

[2 points] void rm_space(char *s)

removes whitespace characters from the beginning and the ending s

[5 points] int find(char *h, char *n)

returns the index of the first occurence of n in the string h or -1 if it isn't found.

[5 points] char *ptr_to(char *h, char *n)

returns a pointer to the first occurence of n in the string h or NULL if it isn't found

[4 points] is_empty(char *s)

returns 1 if s is NULL, consists of only the null character ('') or only whitespace. returns 0 otherwise.

[5 points] str_zip(char *s1, char *s2)

Returns a new string consisting of all of the characters of s1 and s2 interleaved with each other. For example, if s1 is "Spongebob" and s2 is "Patrick", the function returns the string "SPpaotnrgiecbkob"

[5 points] void capitalize(char *s)

Changes s so that the first letter of every word is in upper case and each additional letter is in lower case.

[5 points] int strcmp_ign_case(char *s1, char *s2)

Compares s1 and s2 ignoring case. Returns a positive number if s1 would appear after s2 in the dictionary, a negative number if it would appear before s2, or 0 if the two are equal.

[3 points] 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"

[5 points] 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.

[5 points] 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.

[5 points] ends_with_ignore_case(char *s, char *suff)

returns 1 if suff is a suffix of s ignoring case or 0 otherwise.

[5 points] char *repeat(char *s, int x, char sep)

Returns a new string consisting of the characters in s repeated x times, with the character sep in between. For example, if s is the string all right, x is 3, and sep is , the function returns the new string all right,all right,all right. If s is NULL, the function returns NULL. It is up to the caller to free any memory allocated by the function.

[5 points] char *replace(char *s, char *pat, char *rep)

Returns a copy of the string s, but with each instance of pat replaced with rep, note that len(pat) can be less than, greater than, or equal to len(rep). The function allocates memory for the resulting string, and it is up to the caller to free it. For example, if we call replace("Fiore X", "X", "sucks"), what is returned is the new string Fiore sucks (but remember, pat could be longer than an individual character and could occur multiple times).

[5 points] char *str_connect(char **strs, int n, char c)

Returns a string consisting of the first n strings in strs with the character c used as a separator. For example, if strs contains the strings {"Washington", "Adams", "Jefferson"} and c is '+', the function returns the string "Washington+Adams+Jefferson"

[5 points] void rm_empties(char **words)

words is an array of string terminated with a NULL pointer. The function removes any empty strings (i.e., strings of length 0) from the array.

[5 points] char **str_chop_all(char *s, char c)

Returns an array of string consisting of the characters in s split into tokens based on the delimeter c, followed by a NULL pointer. For example, if s is "I am ready for a nice vacation" and c is ' ', it returns {"I", "am", "ready", "for", "a", "nice", "vacation", NULL}

Explanation / Answer

Program :

#include <stdio.h>
int len_diff( char s1[], char s2[] ){
int l1 = 0;
char* c = s1;
while( *c != '' ){
l1++;
c++;
}
int l2 = 0;
c = s2;
while( *c != '' ){
l2++;
c++;
}
return l1-l2;  
}
void rm_left_space( char s[] ){
int blankSpaceEndAt = 0;
char* c = s;
while( *c != '' && *c == ' ' ){ c++; blankSpaceEndAt++; }
c = s + blankSpaceEndAt;
while( *c != '' ){
*s = *c;
c++; s++;
}
*s = '';
}
void rm_right_space( char s[] ){
char* c = s;
while( *c != '' ){ c++; }
c--;
//c is now end of string
while( *c == ' ' ){ c--;}
c++;
*c = '';
}
void rm_space( char s[] ){
rm_left_space(s);
rm_right_space(s);
}

int myStrlen(char* s){ //function for calculate length of string,as we cannot use the inbuilt function
int i=0; //loop variable
int length=0; //variable to keep track of length of the string
while(s[i]!='') //run the loop till null character not found
{length++;
i++;
}
return length;
}
//end of method


//start of method
int all_letters(char *s){
int len=myStrlen(s); //length of string s1
int uppercase=0,lowercase=0; //variable to keep track of uppercase and lowercase
int i=0; //loop variable
for(i=0;i<len;i++) //loop till s1 length
{
if(s[i]>='a' && s[i]<='z') //if s1 element is lowercase
lowercase++; //increase lowercase counter
else if(!(s[i]>='a' && s[i]<='z')) //else s1 element is uppercase
uppercase++; //increase uppercase counter
}
if(uppercase==len || lowercase==len) //if either lowercase or uppercase == length of string s1
return 1; //return 1 as all string elements are either lowercase or uppercase
else //else return 0 as elements of strings are the mixture of lowercase and uppercase
return 0;
}
//end of method


//start of method
int num_in_range(char *s1, char b, char t){
int len=myStrlen(s1); //length of string s1

int range=0; //variable for storing no of characters between b and t
int i; //loop variable
for(i=0;i<len;i++) //loop till s1 length
{
if(s1[i]>='b' && s1[i]<='t') //if element of s1 is betwwen b and t,both inclusive
range++; //increase range counter
}
return range; //return range
}
//end of method

//start of method
int diff(char *s1, char *s2){
int len1=myStrlen(s1); //length of s1 string
int len2=myStrlen(s2); //length of s2 string
int diffr=0; //variable for difference
int i,j; //variable for loop
if(len1==len2){ //if s1 length==s2 length
for(i=0;i<len1;i++){
if(s1[i]!=s2[i]) //if element of string1 != string2
diffr++; //increase difference counter
}
}
else if(len1 > len2){ //if s1 length>s2 length
for(i=0;i<len2;i++){ //loop till s2 length
if(s1[i]!=s2[i]) //if element of string1 != string2
diffr++; //increase difference counter
}
diffr=diffr+len1-len2; //add the extra length of s1 length
}
else{ //s1 length<s2 length
for(i=0;i<len1;i++){ //loop till s1 length
if(s1[i]!=s2[i]) //if element of string1 != string2
diffr++; //increase difference counter
}
diffr=diffr+len2-len1; //add the extra length of s2 length
}
return diffr; //return difference of both string
}
//end of method

//start of method
void shorten(char *s, int new_len){
int len=myStrlen(s);
if(len<=new_len) //if string len is less than given length,string will remain unchanged
printf("string is unchanged");
else{int i; //else
for(i=new_len;i<len;i++)
s[i]=''; //terminate the string by putting null character,i.e string is shortened
  
}
}
//end of method

int main(){
char s1[] = "asdasdasd";
char s2[] = "fddf";
char string1[]="AbNUraG"; //string1 intialization
char string2[]="ANSHua"; //string2 intialization
printf("%d ",all_letters(string1)); //calling all_letters(char *s) function
printf("%d ",num_in_range(string1,'b','t')); //calling num_in_range(char *s1, char b, char t) function
printf("%d ", diff(string1,string2)); //calling diff(char *s1, char *s2) function
shorten(string1,4); //calling void shorten(char *s, int new_len) function
printf("%d ",myStrlen(string1));
printf("%d ",len_diff( s1, s2 ));
char s[] = " abc";
rm_left_space( s );
printf("%s ", s );
char s3[] = "abc ";
rm_right_space(s3);
printf("%s ", s3 );
char s4[] = " abc ";
rm_space(s4);
printf("%s ", s4 );
return 0;
}