You are not logged in.
Here are the pre-processors:
#include <stdio.h>
#include <string.h>
Here are the global variables:
char output[999];
Here's the function to sort string to reverse order (I THINK THE PROBLEM IS HERE):
char output[999];
char *sort_reverse(const char *str)
{
int i, j = 0;
for (i = strlen(str); i > 0; i--)
{
output[j] = str[i];
j++;
}
return output;
}
And, function int main(void):
int main(void)
{
printf("Reverse of \"Hello\": %s\n", sort_reverse("Hello"));
return 0;
}
The output is:
Reverse of "Hello":
Why is that? how can i fix that?
Last edited by milo64 (2013-03-28 01:45:46)
milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/
Offline
for (i = strlen(str) - 1; i > -1; i--)
because string length in array goes from 0 to strlen(str) - 1, ommiting the NUL terminator. With your code you were copying the NUL character first, and that terminated the string before it even started.
Last edited by kaszak696 (2013-03-27 09:21:35)
'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard
Offline
I can't check it here now (no c compiler on windows...) but I think you are copying the terminating \0 byte as the first character, so your resulting string is terminated immediately.
So I think your function maps like this:
Hello\0 -> \0olle
where the "H" is missing as well, because you do not take care of \0.
It should do this though:
Hello\0 -> olleH\0
Offline
for (i = strlen(str) - 1; i > -1; i--)
because string length in array goes from 0 to strlen(str) - 1, ommiting the NUL terminator. With your code you were copying the NUL character first, and that terminated the string before it even started.
Hmm, i thought strlen()'s code doesn't count the null character because:
size_t strlen(const char *str)
{
int i;
for (i = 0; str[i] != '\0'; i++) ;
return i;
}
the loop should've broke once it hits the '\0' and the i value not counting it.
The man page says that:
The strlen() function calculates the length of the string s, ***excluding the terminating null byte ('\0').***
Last edited by milo64 (2013-03-27 10:06:10)
milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/
Offline
Yes, indeed, but that wasn't my point. My point was that the char array starts at 0, so the string is contained in the space str[0] to str[strlen(str) - 1], and the NUL is in str[strlen(str)], which was the first character your initial code reads. As Ramses pointed out, your output array started with NUL, so it appeared to be an empty string.
'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard
Offline
str = "Hello"
strlen(str) = 5
str[0] = H
str[1] = e
str[2] = l
str[3] = l
str[4] = 0
str[5] = \0
Edit: too slow.
Last edited by skanky (2013-03-27 10:14:40)
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
In addition to the above corrections, you'll need to also set the null byte yourself on the ouput string:
output[strlen(str)] = '\0';
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
In addition to the above corrections, you'll need to also set the null byte yourself on the ouput string:
output[strlen(str)] = '\0';
When i examined the program in gdb, the entire output array was already nulled. Is it reliable to expect it to be nulled every time?
'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard
Offline
I suppose. I forget what the rules are for uninitialized arrays. Uninitialized "regular" varibales can not be assumed to be zeroed - they often are not. Perhaps it is safe - but I'd never trust it.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
It's safe in this case because output is static (defined outside of any function). In this case arrays are initialized by setting all their elements to 0. Only automatic variables, i.e. those defined within a function without the keyword "static", are ever uninitialized.
Source is my memory and I haven't looked it up so don't sue me.
Disclaimer: Just to be clear, it is definitely a good idea to add the terminator explicitly and I would not suggest leaving it off even if you know it is unnecessary. It makes verification easier and allows you to use the same algorithm for a truly uninitialized string. Also here you would likely run into problems if you try to call the function multiple times.
Last edited by Trent (2013-03-27 22:07:42)
Offline
Anyways, it seem to work now, so... it's solved. thanks all.
milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/
Offline
Instead of the global char output[999] wich is really big, you could allocate a perfectly sized array with calloc - like this in the sort_reverse function:
char *output = calloc(strlen(input_string), sizeof(char));
and in main program:
char *reversed_string = sort_reverse("Whatever");
printf("%s\n", reversed_string);
//Remember to free reversed_string with
free(reversed_string);
//Since its allocated in the heap with calloc()
Try check man-page for calloc, it will zero out the bytes aswell, then you wont have to worry about setting the last element to '\0'.
NOTE: you have to iterate with "strlen(input_string) - 1" otherwise you will overwrite the needed '\0'
EDIT: As strlen excludes the '\0' in original string, then you have to make space for that by adding one to strlen return value so:
char *output = calloc(strlen(input_string) + 1, sizeof(char));
Last edited by Boogie (2013-04-05 09:46:12)
Offline