I wrote a code to reverse a string, but the strlen
function is giving me the wrong length of string, that's why the reversing of the string is not done properly. Here is the code I wrote:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reversestring(char string[], int start, int end);
int main() {
char str[500];
int n;
n = strlen(str);
reversestring(str, 0, n - 1);
printf("%d\n", n);
printf("The reverse string is %s", str);
return 0;
}
void reversestring(char string[], int start, int end) {
printf("enter the string:\n");
scanf("%s", string);
int temp;
while (start < end) {
//printf("insidewhile\n");
temp = string[start];
string[start] = string[end];
string[end] = temp;
start++;
end --;
}
}
strlen()
can´t give you the length of a string, when its argument does not point to a valid string, which is the case on your example:
char str[500];
int n;
n = strlen(str);
str
isn´t initialized with a string.
Providing a pointer to a char
array which doesn´t contain a string as argument to strlen()
causes undefined behavior.
Also strlen()
doesn´t return an int
. It´s return value is of type size_t
.
Also use fgets()
instead of scanf()
when input a string. It is a little bit more safe.
Solution:
Let the string get entered in main()
into str
, then use strlen()
and thereafter call the reversestring()
function:
char str[500];
size_t n;
printf("enter the string:\n");
fgets(str,500,stdin);
n = strlen(str);
reversestring(str, 0, n-1);
I also edited the function declaration and the printf()
accordingly to take care of the size_t
type.
Here is the full code ( Online Example ):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reversestring(char string[], size_t start, size_t end);
int main()
{
char str[500];
size_t n;
printf("enter the string:\n");
fgets(str,500,stdin);
str[strcspn(str, "\n")] = 0; // removing trailing newline from fgets
n = strlen(str);
reversestring(str, 0, n-1);
printf("%zu\n", n);
printf("The reverse string is %s", str);
return 0;
}
void reversestring(char string[], size_t start, size_t end)
{
int temp;
while(start < end)
{ //printf("insidewhile\n");
temp = string[start];
string[start] = string[end];
string[end] = temp;
start++;
end --;
}
}
Output:
enter the string:
helloworld
10
The reverse string is dlrowolleh
You should use strlen
after the initialization of str
and set a correct size of third argument of reversestring
:
size_t max_length = 128;
reversestring(str, 0, max_length);
n = strlen(str);
You have no string in the declared character array
{ char str[500];
int n;
The character array is not initailzied. So this call
n = strlen(str);
results in undefined bahavior.
This code snippet from the function reversestring
printf("enter the string:\n");
scanf("%s", string);
shall be outside the function and used before the function is called.
The function should be declared at least like
char * reversestring( char string[], size_t n );
There is no need to declare the function with three parameters like you did
void reversestring(char string[], int start, int end);
because a call to this function can be substituted for the call of the previous shown function like
reversestring( string + start, end - start + 1 );
Also pay attention to that using the format %s
does not allow to enter a sentence. Moreover using this format specifier with the function scanf
is unsafe, And the function strlen
has the return type size_t
.
Here is the function definition
char * reversestring( char s[], size_t n )
{
for ( size_t i = 0; i < n / 2; i++ )
{
char c = s[i];
s[i] = s[n-i -1];
s[n-i-1] = c;
}
return s;
}
Here is a demonstrative program.
#include <stdio.h>
#include <string.h>
char * reversestring( char s[], size_t n )
{
for ( size_t i = 0; i < n / 2; i++ )
{
char c = s[i];
s[i] = s[n-i -1];
s[n-i-1] = c;
}
return s;
}
int main(void)
{
enum { N = 500 };
char s[N];
printf( "Enter a string: " );
fgets( s, N, stdin );
s[ strcspn( s, "\n" ) ] = '\0';
size_t n = strlen( s );
puts( reversestring( s, n ) );
char *p = strchr( s, ' ' );
if ( p != NULL && ( p = strchr( p + 1, ' ' ) ) != NULL )
{
reversestring( s, p - s );
puts( s );
}
return 0;
}
Its output might look for example like
Enter a string: Hello Shivam Gupta
atpuG mavihS olleH
Shivam Gupta olleH
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.