Reputation: 3
I wrote the following code to reverse a string:
#include <stdio.h>
#include <strings.h>
void reverse(char* str)
{
int size = strlen(str)+1;
printf("%d\n",size);
char* rev = malloc(size*sizeof(char));
int i,j;
printf("Contents of rev as it is being filled: ");
for(i=size, j=0 ; i>=0 ; i--, j++)
{
//printf("%c\n", str[i]);
rev[j] = str[i];
printf("%c", rev[j]);
};
printf("\n");
printf("%s\n",str);
printf("Result of trying to print rev after it has been made : %s\n",rev);
}
int main()
{
reverse("Hello, World");
return 0;
}
When the contents of the reversed string are printed, they are correct. However, when I try to print rev after the loop I get nothing. When is this happening?
Upvotes: 0
Views: 79
Reputation: 30813
Here is the modified code which I tested as working
void reverse(char* str)
{
int size = strlen(str); //read 1. remove +1 here
printf("%d\n",size);
char* rev = malloc(size*sizeof(char) + 1); //read 2. add +1 here
int i,j;
printf("Contents of rev as it is being filled: ");
for(i=size-1, j=0 ; i>=0 ; i--, j++) //read 3. gives -1 here
{
//printf("%c\n", str[i]);
rev[j] = str[i];
printf("%c", rev[j]);
};
rev[j] = '\0'; //read 4. gives '\0' at the end
printf("\n");
printf("%s\n",str);
printf("Result of trying to print rev after it has been made : %s\n",rev);
}
There are some things to note:
strlen(str)
(without having +1) should be enough to declare the size of your string.%s
, should be increased by 1, this is to add '\0'
character, which is the indicator of termination of printing string. This is just how printf
using %s
works.str[size-1]
; because str size is size, the offset should be one less than the size.Result in my PC:
Upvotes: 3
Reputation: 4750
In your program you have copied the last '\0'
as the first character of rev
; as a result when you print rev
it shows nothing. Besides, you have to add the null character '\0' at the end of rev
. That is the solution of your problem.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void reverse(char* str)
{
int size = strlen(str);
printf("%d\n",size);
char* rev = (char*)malloc(size*sizeof(char));
int i,j;
printf("Contents of rev as it is being filled: ");
for(i=size-1, j=0 ; i>=0 ; i--, j++)
{
rev[j] = str[i];
printf("%c", rev[j]);
};
rev[j] = '\0';
printf("\n");
printf("%s\n",str);
printf("Result of trying to print rev after it has been made : %s\n",rev);
}
Upvotes: -1
Reputation: 106082
i = size
is out of bound array access. Change it to i = size-2
to skip the '\0'
character at the end of the string and you need to null terminate the reversed string.
for(i=size-2, j=0 ; i>=0 ; i--, j++)
{
//printf("%c\n", str[i]);
rev[j] = str[i];
printf("%c", rev[j]);
}
rev[j] = '\0';
Upvotes: 3
Reputation: 7324
You are reversing the range [0, size]
which includes the null terminator. So rev[0]
is the null terminator, making it print nothing. You need to reverse the range [0, size-1]
and then add the null terminator to the end. size
in this case is strlen(str)
rather than strlen(str)+1
.
Upvotes: 2