Write a program that reads a string from the keyboard The st

Write a program that reads a string from the keyboard. The string may or may not have trailing white space (for the purpose of this assignment we define white spaces to be \"normal\" spaces and tabs). Pass the string to a function that removes the white spaces from the beginning and the end of the string. Print out the string. For ease of testing/looking at the result, the output string has to be put between three characters on the front and back, when printing it out. Steps to take Identify and document the input(s) and the output(s) Describe the algorithm that solves the problem Write the C program that solves the problem

Solution

1)

Inputs - a char array or string ,length of string

Output - remove trailing and leadig spaces and print the new string with 3 chars at front and back

3)

#include<stdio.h>

int main()

{

char *str;

printf(\"enter string :\");

scanf(\"%s\",&str);

int n=str.length;

//remove trailing spaces

if(str[n-1]==\' \')

{

str[n-1]=\'\\0\';

i=n-2;

while(str[i]==\' \')

{

str[i]=\'\\0\';

i--;

}

}

//remove leading spaces

if(str[0]==\' \')

{

str[0]=str[1];

i=1;

while(str[i]==\' \')

{

for(int j=i;j<str.length;j++)

{

str[j]=str[j+1];

}

i++;

}

}

//append 3 chars front and back

str=\"***\"+str+\"***\";

printf(\"\ %s\",str);

return 0;

}

 Write a program that reads a string from the keyboard. The string may or may not have trailing white space (for the purpose of this assignment we define white
 Write a program that reads a string from the keyboard. The string may or may not have trailing white space (for the purpose of this assignment we define white

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site