Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment, you may not use any of the C-string library functions such

ID: 3597874 • Letter: F

Question

For this assignment, you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc. You must write your own. You might consider writing helper functions to do tasks that many of these functions require, e.g. finding the last character of the string, and then use those helper functions where convenient.

This function should reverse the words in a string. A word can be considered to be any characters, including punctuation, separated by spaces (only spaces, not tabs, etc.). So, for example, if s is “The Giants won the Pennant!” the function should change s to “Pennant! the won Giants The”

void reverseWords(char *s)

implement this correctly and do it without using any extra memory, e.g. another array. Note: this is one of the classic coding questions given during programming interviews.

Explanation / Answer

so baiscally algo to do this will be

Reverse each word in inplace

so a string like "abc pqr xyz" will become "cba rqp zyx"

then reverse whole string inplace

so it will be "xyz pqr abc"

here is the code

I will be writting one helper method which will reverse a given string

void reverse(char *start, char *end)
{
char temp;
while (start< end)
{
temp = *start;
*start = *end;
*start++;
*end = temp;

*end--;
}
}

void reverseWords(char *s)
{
char *begin = s;
char *temp = s;
  
// reverse each word in place
while( *temp )
{
temp++;
// reverse last word
if (*temp == '')
{
reverse(begin, temp-1);
}
// reverse word on space
else if(*temp == ' ')
{
reverse(begin, temp-1);
begin = temp+1;
}
}
  
// reverse whole string
reverse(s, temp-1);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote