Just an additional answer, be very careful with buffer overflow issues. Also a minor detail, you don't really need a len
variable.
Below a commented code showing a way to deal carefully with memory writing.
#include <stdio.h>
// Here a way to use constants both as integer and strings
// See https://stackoverflow.com/questions/5459868
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
// Let's define a max length
#define MAX_STRING_LENGTH 10
void main()
{
char sptr[MAX_STRING_LENGTH + 1];
char *sptr1=sptr,*sptr2=sptr;
char swap;
printf("Enter a string (" STR(MAX_STRING_LENGTH) " at most): ");
// Here, limit the input to sptr size - 1
// (let the last index for the null character)
// Example : "%10s" means "at most 10 characters, additional ones
// will be removed."
scanf("%" STR(MAX_STRING_LENGTH) "s",&sptr);
// Finding the last character BEFORE the NULL character
while(*(sptr2+1) != '\0') sptr2++;
// Swaping
while (sptr2 > sptr1)
{
printf("\t-> swaping %c <-> %c\n", *sptr1, *sptr2);
swap=*sptr1;
*sptr1=*sptr2;
*sptr2=swap;
sptr1++,sptr2--;
}
printf("Result : [%s]\n",sptr);
}
Examples (strings with odd and even length):
user:~$ ./a.out
Enter a string (10 at most): abc
-> swaping a <-> c
Result : [cba]
user:~$ ./a.out
Enter a string (10 at most): abcd
-> swaping a <-> d
-> swaping b <-> c
Result : [dcba]
user:~$ ./a.out
Enter a string (10 at most): abcdefghijklmnopqrstuvwxyz
-> swaping a <-> j
-> swaping b <-> i
-> swaping c <-> h
-> swaping d <-> g
-> swaping e <-> f
Result : [jihgfedcba]