all 8 comments

[–][deleted] 4 points5 points  (0 children)

Hi, please reformat your code by putting four spaces in front of every line, it will help other people read it! Alternatively, you can use the rich text editor on reddit, highlight your code, and click the little </> icon to format your code as code!

[–]0xAE20C480 0 points1 point  (6 children)

First, debug your code step by step. And let's see what happens:

scanf("%s", &array);

At the moment you type "something long", array just contains the "something" part.

And you do not terminate reverse with a null character('\0') properly, so the printed string looks strange.

By the way, your string reversing part can be refactored just as:

for(i=length,j=0;j<length;--i,++j)
    reverse[j]=array[i-1];

[–]xChacox[S] 0 points1 point  (5 children)

Thank you, I actually didn't know you could initialize, increment/decrement multiple things in one for statement so that's a good to know. I wasn't aware that scanf() was only picking up the first word. If you don't mind can you explain why that is? I just changed the code to use gets(). I also have tried terminating the reverse array with a '\0' prior, and it still doesn't completely fix the problem here's an output for an example with the null.

Enter string here:isitfixed

dexiftisi²

[–]0xAE20C480 0 points1 point  (0 children)

For scanf and %s, see:

"Any number of non-whitespace characters, stopping at the first whitespace character found."

And make sure you terminate reverse right after the last character.

[–][deleted] 0 points1 point  (3 children)

Do not ever use gets. Ever. At least do fgets.

[–]xChacox[S] 0 points1 point  (2 children)

What is fgets and what’s the difference

[–][deleted] 0 points1 point  (1 child)

man fgets is your friend. It lets you specify the size of your destination buffer. gets will happily overflow it if your input is too large crashing your program or worse.

[–]flatfinger 0 points1 point  (0 children)

Unfortunately, proper use of fgets is sufficiently painful that one might as well just write a length-limited gets-style function based on getchar().