I have the following code where Index is defined as length- 1. It prints an extra space in the beginning if I dont include the - 1. Why is the -1 required in the code?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char s[200];
int index, lenght;
printf("Input a string\n");
gets(s);
// Calculating string length
length = strlen(s);
index=length-1;
while (index >=0) {
printf("%c", s[index]);
index--;
}
printf("\n");
return 0;
}
转载于:https://stackoverflow.com/questions/53038637/c-reverse-string