Write a program that reads a string from the keyboard. The string may or may not
ID: 3935198 • Letter: W
Question
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 problemExplanation / Answer
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]='';
i=n-2;
while(str[i]==' ')
{
str[i]='';
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;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.