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

p\"Create the file reverse.c where you will write your program. Your program wil

ID: 672673 • Letter: P

Question

p"Create the file reverse.c where you will write your program.

Your program will read in a single token from standard input. You will then display the reverse of this string as shown in the examples below.

You should use malloc to allocate a new string which will hold the reverse value. Make sure to include the NULL character in your calculations for how much memory you will need. Also, make sure to call free on any allocated memory you have created.

Here is how your program should be compiled and executed

Here is another run of your program

please help the code needs to be in c code not java or c++.

Explanation / Answer

#include <stdio.h>

int main(void) {
  
   char str[255];
   char *rev;
   char ch;
   int i=0, j, k;
  
   printf("Enter a single word: ");
   while(1) {
          
       scanf("%c", &ch);
      
       if(ch == ' ') {
           str[i] = '';
           break;
       }
       str[i] = ch;
       i++;
   }
  
   rev = (char *)malloc(sizeof(char)*i);
  
   rev[i] = '';
   k=0;
   for(j=i-1; j>=0; j--) {
       rev[k] = str[j];
       k++;
   }
  
   printf(" The reverse of "%s" is "%s"", str, rev);
  
   return 0;
}