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 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. num_in_range(char *s1, char b, char t)

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

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

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

Solution

#include <stdio.h>

//start of method
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]!=\'\\0\') //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]=\'\\0\'; //terminate the string by putting null character,i.e string is shortened
  
}
}
//end of method
int main() {
   // your code goes here
   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));
  
   return 0;
}
//end of code

/***********OUTPUT********
0 //as string1 is not on either lowercase or uppercase
2 //as only 2 characters of string1 is in between b and t
5 //difference between string1 and string2
4 //string1 is shorten by 4 hence length of new string1 is 4
**********OUTPUT*********/

//Please let me know in case of any doubt,Thanks.

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