Struggling with Reverse of String

This is a discussion on Struggling with Reverse of String within the C Programming forums, part of the General Programming Boards category; So I am writing a program that reverses any given input of strings..
however it looks weird..it produces a new ...

Struggling with Reverse of String

So I am writing a program that reverses any given input of strings..
however it looks weird..it produces a new line under the given input then prints the reverse of the string. I am also able to type next to the reversed string which shouldn't happen. I should be able to type in the line under it. Help much appreciated!

Is that how it looks? Is '\0' equivalent to null? How would EOF relate to this '\0' char? Is this '\0' sign at the end of every line, or the end of the entire input, say a whole paragraph of 5 lines? What would Z[5], Z[6],....Z[100] contain? I hope this can be clarified. Thanks!

If you used a for loop to put the letter into the char array, it might go in that way. It could also go in reverse order, depending on how the for loop or while loop, is set up.

Some functions in C will put an end of string '\0' marker in for you, others won't. It's up to you as the programmer to ensure what's what.

What the rest of Z[] might contain all depends:

1) Global char array's on the stack, (before any functions), are initialized to "".
2) Char array's inside a function and malloc'ed, may have any random value.
3) Char array's inside a function and calloc'ed, will be all set to "" (the empty string).

Yes, you have it more or less right: entering "abc" followed by pressing the enter key would result in the data 'A', 'B', 'C', '\n', '\0' being stored in whatever string you read the input into. (Note my quotes.)

A '\0' occurs at the end of every string. It's there so that functions like strcmp() and printf() know where the string ends. So how often it occurs depends on how many strings you have. If you had one string for every line in the file, for example (a common occurence), then you'd also have one '\0' at the end of every string. If you put all of the data from a file into one big string (better make sure you have enough space!), there would only be one '\0', at the end of the data.

Note that this means you can truncate a string just by adding a '\0' to it.

Code:

char greeting[] = "Hello";
greeting[2] = '\0';
// Now greeting effectively contains "He", according to just about every C and C++ function

This '\0' character is sometimes called the NULL terminator; it's equivalent in all ways to the number 0, basically. (Except that '\0' has type char instead of type int.) So you could use, for example,

Code:

string[i] = 0;

to terminate a string.

Do be careful with your use of the word "NULL", however: when referring to pointers, it's not the same thing, really. A pointer is NULL when it doesn't point to anything. Almost all of the time, this is equivalent to saying that the pointer has the value 0, so it would seem quite similar to the NULL terminating character '\0'. But it's not the same; '\0' is a char, NULL is of a pointer type. (0 can be used in both cases.)

EOF is another matter. EOF is actually not a character; it's of type int. EOF is used to signify that the end of a stream has been reached. However, you shouldn't try to stuff EOF into a character, because then EOF will correspond with an actual valid character in the file. For example, if EOF is -1 (which it frequently is), and you're reading unsigned chars, that will become 255, which could actually occur in the file. (And if it did, your program would think it had reached the end of the file when it actually had not.)

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

It would simply mark the end of a string, like how a full stop marks the end of a sentence.

Originally Posted by dnguyen1022

Whats the difference between that and string[i] = '\0'?

0 is an integer const, '\0' is a character constant. They both are of type int and have the same value. (However, if you ever use C++, note that '\0' would then be of type char.)

Originally Posted by dnguyen1022

What if I wanted to use the char 0('0')?
Would I do string[i] = '0'?

Yes since '0' is also a character constant, but it does not have the value of 0 (in ASCII, it has the value of 48).

Originally Posted by dnguyen1022

Also with that hello example, would that new array look like
'h', 'e', '\0', 'l', 'l', 'o'.

Yes.

Originally Posted by dnguyen1022

Or having a '\0' simply "wipes" everything after it?

No.

Originally Posted by dnguyen1022

Also if I had an array of size 10, and I gave it 'a', 'b', 'c'. array[3] being '\n', array[4] being '\0'. What is within array[5], ..[6], up to [10]?

If you initialise that array with "abc\n" then array[5] onwards would be '\0'. However, if you assign characters individually, then array[5] onwards would contain whatever was already there, which could be "garbage" if the array was not initialised.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

The character '0' represents the digit zero. The character '\0' represents the numerical zero and is the one that acts as a sentinel value for the end of a C-string. The bit patterns for these two characters are never equivalent and one will not do the job of another.

Now, for your other question:
string[i] = 0;
This statement relies on type coercion. In short, even though the literal zero is written the compiler will store the null terminator as that is the proper representation.

Relying on type coercion is not smart, imo, if you are using even a remotely type strict language. This case is not one of them, but there are situations where type coercion should be avoided to prevent error. It's best not to fall into any habits in the early stages of your C programming.