title: Reverse string in malloc
body: <p>I need define a “word” in this question to be any sequence of characters that doesn’t contain a space or null character.
  For example, the string <code>“Hello World”</code> would contain 2 words. However, it is actually possible for a word to be empty
  i.e., zero chars.
  A sentence would be a series of words that are separated by 1 space character. So <code>“Hello World”</code> would be a
  sentence of two words. The goal of <code>ReverseSentence</code> would be to reverse the sentence in terms of word.
  Right now, I am having an error whereby programs proceed to call function and prints out a1 to a5. Upon reaching a5, it seems that program abort and core dumped. If i replace blank with space, it will read in the previous input and replace according to number of space.
  Where am i going wrong? </p>
  
  <p><strong>ReverseSentence.c</strong></p>
  
  <pre><code>#include &lt;stdlib.h&gt;  /* malloc */
  #include &lt;string.h&gt;  /* strcat, strcpy */
  
  void ReverseSentence(char *str)
  {
      char *newSentence;
      int i, j, start, len;
      /* contains the string length of the input */
      len = strlen(str);
      /* position or index in the array */
      start = strlen(str);
      /* malloc */
      newSentence = malloc(len + 1);
  
      /* loop checks from the right of the sentences */
      for (i = len; i &gt;= 0; i--) {
          /* if index reach the array with a space or zero */
          if (str[i] == ' ' || i == 0) {
              /* allocates memory */
              char *word = malloc((start - i) + 1);
              int c = 0;
  
              if (i == 0) 
                  /* index remains same */
                  j = i;
              else
                  j = i + 1;
  
              /* j smaller or equal than the start position */
              for (; j &lt;= start; j++) {
                  /*do a incremental*/
                  word[c++] = str[j];
              }
              /* hits a null char */
              word[c] = '\0';
              /* string concatenate */
              strcat(newSentence, word);
              /* if index hits a space */
              if (str[i] == ' ')
                  strcat(newSentence, " "); /* concatenate space to newSentence */
              else
                  strcat(newSentence, "\0");
              start = i - 1;
  
              /* free memory */
              free(word);
          }
      }
      newSentence[len] = '\0';
      /* string copy */
      /* str is destination, newSentence is the source */
      /* copy new string to original string */
      strcpy(str, newSentence);
      /* free memory */
      free(newSentence);
  }
  </code></pre>
  
  <p><strong>main.c</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "ReverseSentence.h"
  
  int main()
  {
      char a1[] = "Hello World ";
      char a2[] = "abcdefghi ";
      char a3[] = " ";
      char a4[] = "C programming is a dangerous activity";
      char a5[] = "a "; /* a sentence with only empty words */
      ReverseSentence(a1);
  
      printf("Test case 1:\"%s\"\n", a1); /* prints "World Hello" */
      ReverseSentence(a2);
  
      printf("Test case 2:\"%s\"\n", a2); /* prints "abcdefghi" */
      ReverseSentence(a3);
  
      printf("Test case 3:\"%s\"\n", a3); /* prints "" */
  
      ReverseSentence(a4);
      printf("Test case 4:\"%s\"\n", a4); /* prints "activity dangerous a is pro Cgramming" */
  
      ReverseSentence(a5);
      printf("Test case 5:\"%s\"\n", a5); /* prints " " */
  
      return 0;
  }
  </code></pre>
  
  <p><strong>EDIT: new version</strong></p>
  
  <pre><code>void ReverseSentence(char *str)
  {
      /* holder */
      /* pointer to char */
      char *newSentence;
      int i, start, len, lastindex, size;
  
      /* contains the string length of the input */
      len = strlen(str);
      lastindex = strlen(str);
      /* starting position */
      start = 0;
      i = 0;
      /* malloc */
      newSentence = malloc(sizeof(char) * strlen(str));
  
      while (i &gt;= 0) {
          for (i = len - 1; str[i] != '\0' &amp;&amp; str[i] != ' '; i--) {
              lastindex--;
          }
  
          /* number of chars in string size */
          size = len - lastindex;
          /* Copy word into newStr at startMarker */
          strncpy(&amp;newSentence[start], &amp;str[lastindex], size);
  
          /* pointer move to right */
          start = start + size;
          /* Space placed into memory slot */
          newSentence[start] = ' ';
          /* start position moves by 1 towards the right */
          start = start + 1;
          /* pointer at len moves to left */
          lastindex = lastindex - 1;
          /* lastindex moves to where len is */
          len = lastindex;
      }
  
      /* Copy new string into old string */
      for (i = 0; str[i] != '\0'; i++) {
          str[i] = newSentence[i];
      }
  
      /* free memory */
      free(newSentence);
  }
  </code></pre>