This question asks you to write three string functions There

This question asks you to write three string functions. There is no need for temporary arrays as the space in the parameters is sufficient. Note there is also no need for I/O (printf or scanf) in these functions. As a general design principle, I/O should be restricted to a few functions which are specifically used for this purpose. Write a function to add extra blanks to the left end of a string to make it at least length n. If the string is already n characters or longer, do not change the string. Assume the prototype is void padLeft(char a[], int n); Write a function which shortens a string to n characters. If the string is already shorter than n, the function should not change the string. Assume the prototype is void truncate (char a[], int n); Write a function which capitalizes the first letter in every word. Assume the first letter is any letter at the beginning or preceded by a blank. All other letters should be turned into lowercase. You can use the library functions topper and to lower which work on one char and return a char value (without changing its parameter). Assume the prototype is void capitalizeWords(char a[]);

Solution


/*a) left padding with blank space*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void padleft(char a[],int n)
{
int i,j,s=strlen(a);// getting the length of the string a
if(s==n ||s==0){
   printf(\"%s\",a);

return;

}
else{
    char b[50];
   strcpy(b,a);//storing a string into b

b[s]=\'\\0\';
   for(j=0;j<n;j++)
        a[j]=\' \'; //appending n spaces to string a
   strcat(a,b); // adding the original string to a

printf(\"%s\",a);
   }
}
main(){
   char a[]={\"a,b,c,d\"};
   padleft(a,5);
};


/* b) Truncating the string with specified number of characters*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void truncate(char a[],int n)
{
int i,s=strlen(a);// getting the length of the string a
if(s==n ||s==0||s<n)
   printf(\"%s\",a);
   a[s-n]=\'\\0\'; //truncating the string to n characters
   printf(\"%s\",a);
}
main(){

char a[]={\"a,b,c,d,e,f\"};

truncate(a,3);
};

/*C.Captalizing the first letter of the word using toupper */


#include <stdio.h>
#include <string.h>
void capitalizeWords(char a[]){   
int i;
int l = strlen(a);

a[0]=toUpper(a[0]);
for (i=1;i<l;i++){
if (isalpha(a[i]) && a[i-1] == \' \'){

       // picking only first letters of a word.
a[i]= toupper(a[i]);
}
}
}

main(){
char a[] = \"text with lowercase words.\";
capitalizeWords(a);
printf(\" %s\",a);
};

 This question asks you to write three string functions. There is no need for temporary arrays as the space in the parameters is sufficient. Note there is also
 This question asks you to write three string functions. There is no need for temporary arrays as the space in the parameters is sufficient. Note there is also

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site