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. 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

Solution

#include <stdio.h>

int len_diff( char s1[], char s2[] ){
   int l1 = 0;
   char* c = s1;
   while( *c != \'\\0\' ){
       l1++;
       c++;
   }
   int l2 = 0;
   c = s2;
   while( *c != \'\\0\' ){
       l2++;
       c++;
   }
   return l1-l2;  
}

void rm_left_space( char s[] ){
   int blankSpaceEndAt = 0;
   char* c = s;
   while( *c != \'\\0\' && *c == \' \' ){ c++; blankSpaceEndAt++; }
   c = s + blankSpaceEndAt;
   while( *c != \'\\0\' ){
       *s = *c;
       c++;   s++;
   }
   *s = \'\\0\';
}

void rm_right_space( char s[] ){
   char* c = s;
   while( *c != \'\\0\' ){ c++; }
   c--;
   //c is now end of string
   while( *c == \' \' ){ c--;}
   c++;
   *c = \'\\0\';
}

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;
}

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 Inclu
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 Inclu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site