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

/* takes a string and reverses it without making a copy of the original array *

ID: 3827833 • Letter: #

Question

/* takes a string and reverses it without making a copy of the original array

* My algorithm:
* (1) get word
* (2) get size of word
* (3) send poiinter to function
* (4) use temps to revesrse
*/

#include
#include
#define MAXLENGTH 1000

/* takes string and string size and reverses it suiing poointers */
void reverseWord(char *word, int size)
{
   int ptrArray[size];
   for(int i = 0; i < size; i++)
   {
       ptrArray[i] = word+i; // collects addresses in an array
   }
  
   int j = size-1;
   for(int i = 0; i < size; i++)
   {
       word[i] = &ptrArray+j; // assigns contents in reverse // i just relised this will not work because contents will change as it runs.
       j--;
   }
  
}

int main(int argc, char **argv)
   {
   /* get String */
   char word[MAXLENGTH];
   printf("Enter a string ");
   scanf("%s",word);
  
   /* get size of string */
   int size = strlen(word);
   printf("size = %i ",size);
  
   /* reverse string */
   reverseWord(word, size);
  
   printf(" Your string reversed is '%s' ",word);
   return 0;
}

8. In Q3, you wrote a program that prints out the characters of a word in reverse. Now, write a program that will reverse the characters of the word in place, e. using the same character array that you used to store the word.

Explanation / Answer

You are not dpoing inplace reverse. Here is the fixed code.

#include <stdio.h>
#include <string.h>
#define MAXLENGTH 1000
/* takes string and string size and reverses it suiing poointers */
void reverseWord(char *word, int size)
{
int c, i, j;
for (i = 0, j = size - 1; i < j; i++, j--)
{
c = word[i];
word[i] = word[j];
word[j] = c;
}
}
int main(int argc, char **argv)
{
/* get String */
char word[MAXLENGTH];
printf("Enter a string ");
scanf("%s",word);
  
/* get size of string */
int size = strlen(word);
printf("size = %i ",size);
  
/* reverse string */
reverseWord(word, size);
  
printf(" Your string reversed is '%s' ",word);
return 0;
}