I am trying to write a code in C, to reverse a string. Needless to say that using library functions is not an option as I am trying to build a little bit of logic. I am getting a read access violation at line 22. I tried doing this using character arrays as well as string pointer, but both give the same "access violation" result. Can somebody clear the concepts regarding the string access rules using this example?
#include<stdio.h>
#include<stdlib.h>
int main()
{
char reversed_string[21], input[21];// *temp;
printf("Please enter the string(max 20 characters) : ");
gets_s(input, 20);
unsigned short int i, j,length;
i = j=length = 0;
while (input[i] != '\0')
{
length++;
i = i + 1;
}
i = length - 1;
while (i >= 0)
{
reversed_string[j] = input[i];
i = i - 1;
j = j + 1;
}
reversed_string[j] = '\0';
printf("\n The reversed string is : %s.\n\n", reversed_string);
system("pause");
return 0;
}