You are not logged in.

#1 2013-03-27 08:47:26

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

[C] Trying to sort string into reverse order ... Blank. [SOLVED]

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

#2 2013-03-27 08:57:03

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: [C] Trying to sort string into reverse order ... Blank. [SOLVED]

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

#3 2013-03-27 08:59:33

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: [C] Trying to sort string into reverse order ... Blank. [SOLVED]

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

#4 2013-03-27 09:56:29

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Re: [C] Trying to sort string into reverse order ... Blank. [SOLVED]

kaszak696 wrote:
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

#5 2013-03-27 10:10:08

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: [C] Trying to sort string into reverse order ... Blank. [SOLVED]

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

#6 2013-03-27 10:13:50

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [C] Trying to sort string into reverse order ... Blank. [SOLVED]

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. smile

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

#7 2013-03-27 11:27:34

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,445
Website

Re: [C] Trying to sort string into reverse order ... Blank. [SOLVED]

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

#8 2013-03-27 11:31:43

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: [C] Trying to sort string into reverse order ... Blank. [SOLVED]

Trilby wrote:

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

#9 2013-03-27 11:36:22

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,445
Website

Re: [C] Trying to sort string into reverse order ... Blank. [SOLVED]

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

#10 2013-03-27 21:46:34

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [C] Trying to sort string into reverse order ... Blank. [SOLVED]

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

#11 2013-03-28 01:46:14

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Re: [C] Trying to sort string into reverse order ... Blank. [SOLVED]

Anyways, it seem to work now, so... it's solved. thanks all.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#12 2013-04-05 09:13:39

Boogie
Member
Registered: 2012-06-17
Posts: 27

Re: [C] Trying to sort string into reverse order ... Blank. [SOLVED]

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

Board footer

Powered by FluxBB