64 615
Assignments Done
99,1%
Successfully Done
In September 2018

Answer to Question #68919 in C for Atif Haroon

Question #68919
Q. 1: Implement the following function:
surround(char * str, char c)
The first argument is a pointer to a string, whereas the second argument is a
character. When the function returns, the string pointed to by str should be
surrounded by the character in variable c. For instance, if str points to “this”,
and c is ‘X’, then after the function returns, str should point to “XthisX”.
Expert's answer
void surround(char * str, char c)
{
/* step 1 - find end of str */
char* s = str;
while(*s)
{
s++;
}

/* extend str by 2 characters */
s+=2;
/* make end symbol for extended str */
*s = 0;
s--;
/* put ending surrounding symbol */
*s = c;
s--;
/* shift right original str string by one position */
while(s>str)
{
*s = *(s-1);
s--;
}
/* put beginning surrounding symbol */
*str = c;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions