Your code is nice ..........There are two more bugs in my code.....If the first chara is '\n' it will be printed twice.................. I don't check for EOF....
Thank u for u r code........
I didn,t understand what you say about my realloc ... please clarify........
Doh! I guess I left that thought unfinished. I said H/2 - 1, but in retrospect, it should be floor(H/2) bytes. What I was getting at is this:
If H is the maximum size of the heap in bytes, you can only ever have a string that is floor(H/2) bytes in length (counting the null). If you allocate n bytes, then reallocate n+1 bytes, the computer must allocate the n+1, copy everything and free the old n bytes. That means you always need room for a second copy of what you have (plus the extra byte you're asking for). Lets take an example:
If your heap (H) is 1000 bytes and your current string is 499 bytes, when you ask to realloc 500 bytes, the computer leaves your 499 bytes alone, allocates 500 bytes (we're now using 999 bytes), copy the data and free the old 499 bytes. Now lets pretend your string is 500 bytes. You ask to realloc 501 bytes, but that would require a total of 1001 bytes on the heap (too many), so the realloc fails. You can't get beyong floor(H/2) bytes if you realloc(p, size+1).
It's true that realloc may try to simply extend the end of your allocated memory segment, instead of allocating a second, bigger chunk and copying, but that behavior is not guaranteed. Besides, the allocation/garbage collection algorithms are probably OS dependent, so you can't count on there being more floor(H/2) bytes available.