Your algorithm went off the rails, but we can get the train back up on the track. The key is to recognize you have to reverse each WORD in the string preserving whitespace and then output characters in groups of width characters at a time from your output array. If the final group has output that would be beyond the length of the string, output spaces and then the final characters.
That and cleaning up your code, validating all input and getting rid of scanf() that is full of pitfalls for the new C programmer, you could do the following:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXC 128 /* if you need a constant, #define one (or more) */
int main (void) {
char input[MAXC],
output[MAXC];
size_t length = 0, /* strlen returns size_t not int */
width = 0,
i = 0;
/* just reuuse the 'input' buffer to read width and use sscanf() for
* the conversion VALIDATING both by CHECKING THE RETURN.
*/
fputs ("Enter the width of an output line : ", stdout);
if (!fgets (input, MAXC, stdin) || sscanf (input, "%zu", &width) != 1) {
puts ("(user canceled input) or invalid integer input");
return 1;
}
/* read/validate input, trim \n and get length */
fputs ("Enter your text : ", stdout);
if (!fgets (input, MAXC, stdin)) {
puts ("(user canceled input)");
return 1;
}
input[strcspn (input, "\n")] = 0; /* trim \n from end */
length = strlen (input); /* get input length */
putchar ('\n'); /* use putchar() to output a single-character */
/* reverse words in input preserving whitespace */
for (i = 0; input[i];) { /* loop each char in input */
if (isspace (input[i])) { /* if space, copy to output */
output[i] = input[i];
i++; /* increment loop index */
}
else { /* otherwise, not space, get length of word, save index */
size_t wordlen = strcspn (input + i, " \t\n"),
index = i;
if (!wordlen) { /* if wordlen is zero, at end, break */
break;
}
while (wordlen--) { /* reverse loop wordlen filling output */
output[i++] = input[index + wordlen]; /* increment loop index */
}
}
}
output[i] = 0; /* nul-terminate output */
for (i = 0; i < length;) { /* loop while i < length */
size_t j = width; /* counter for width characters */
while (j--) { /* loop width number of times */
if (j + i >= length) { /* if i + j outside string put space */
putchar (' ');
}
else { /* otherwise */
putchar (output[i++]); /* output next char in output */
}
}
putchar ('\n'); /* tidy up with newline */
}
}
(note: you can trim the '\n' and save the length in a single call with input[(length = strcspn (input, "\n"))] = 0;, but up to you)
Also note, the last if-else can be considerably shortened using a ternary operator. That would reduce the final comparison to:
putchar (j + i >= length ? ' ' : output[i++]);
Up to you which you want to use.
Example Use/Output
Now your example output is correctly reproduced, e.g.
$ ./bin/revstr
Enter the width of an output line : 5
Enter your text : Hello there
olleH
ereh
t
Or if you added a character, e.g.
$ ./bin/revstr
Enter the width of an output line : 5
Enter your text : Hello Mickey
olleH
yekc
iM
Look things over and let me know if you have further questions.