Please write the code in C programming language. You may not call functions in s
ID: 3935664 • Letter: P
Question
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, ''.
1. int len_diff(char *s1, char *s2)
Returns the length of s1 - the length of s2
2. void rm_left_space(char *s)
removes whitespace characters from the beginning of s
3. void rm_right_space(char *s)
removes whitespace characters from the end of s
4. void rm_space(char *s)
removes whitespace characters from the beginning and the ending s
Explanation / Answer
#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 main(){
char s1[] = "asdasdasd";
char s2[] = "fddf";
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;
}