For starters the pointer s is uninitialized and has an indeterminate value
char *s;
So the call of the function gets that is unsafe (and the function is not supported by the C Standard)
gets(s);
invokes undefined behavior.
Instead you could write in main for example
char s[100];
fgets( s, sizeof( s ), stdin );
s[ strcspn( s, "\n" ) ] = '\0';
As for the function then take into account that to reverse a string does not mean to output a string in the reverse order. It means to change the string itself.
The function should be declared and defined the following way
char * reversed_string( char *s )
{
if ( *s )
{
for ( char *first = s, *last = s + strlen( s ); first < --last; ++first )
{
char c = *first;
*first = *last;
*last = c;
}
}
return s;
}
And in main the function is called like
puts( reversed_string( s ) );
Here is a demonstration program.
#include <stdio.h>
#include <string.h>
char * reversed_string( char *s )
{
if ( *s )
{
for (char *first = s, *last = s + strlen( s ); first < --last; ++first)
{
char c = *first;
*first = *last;
*last = c;
}
}
return s;
}
int main( void )
{
enum { N = 100 };
char s[N];
printf( "Enter a string: " );
fgets( s, sizeof( s ), stdin );
s[strcspn( s, "\n" )] = '\0';
puts( reversed_string( s ) );
}
If you may not use the standard string function strlen then the function reversed_string can look the following way
char * reversed_string( char *s )
{
if ( *s )
{
char *last = s;
while ( *last ) ++last;
for (char *first = s; first < --last; ++first)
{
char c = *first;
*first = *last;
*last = c;
}
}
return s;
}
swas uninitialised (has no memory allocation) and thatgets()is undefined.