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”.
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!
Learn more about our help with Assignments:
C