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

Write a C program that lets a user enter a statement displays a number of words

ID: 3805006 • Letter: W

Question

Write a C program that lets a user enter a statement displays a number of words in the entered statement and reverses the words of the entered statement Test cases: hope is what defines man are you as happy as I am fall leaves after leaves fall the quick brown fox jumps over a lazy dog Sample code executions: Red entered by a user Enter a statement: hope is what defines man man defines what is hope There are 5 words in the statement Enter a statement: the quick brown fox jumps over a lazy dog dog lazy a overjumps fox brown quick the There are 9 words in the statement

Explanation / Answer

void reverseWords(char *s)

{

  char *word_begin = s;

  char *temp = s; /* temp is for word boundry */

  /*STEP 1 of the above algorithm */

  while( *temp )

  {

    temp++;

    if (*temp == '')

    {

      reverse(word_begin, temp-1);

    }

    else if(*temp == ' ')

    {

      reverse(word_begin, temp-1);

      word_begin = temp+1;

    }

  } /* End of while */

   /*STEP 2 of the above algorithm */

  reverse(s, temp-1);

}

void reverse(char *begin, char *end)

{

  char temp;

  while (begin < end)

  {

    temp = *begin;

    *begin++ = *end;

    *end-- = temp;

  }

}

int main()

{

  char s[] = "i like this program very much";

  char *temp = s;

  reverseWords(s);

  printf("%s", s);

  getchar();

  return 0;

}

You can define any value to array variable c.

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