Here I have a c for loop and another loop that does the same without using && or || http://gist.github.com/1586961 I'm wondering about other implementations or if anybody wants a try at another implementation :)
in your second implementation (the one without using && or ||), you're using i without initializing it.
Both implementations are valid, other than the following issues: -the uninitialized i in the second one -you're also never incrementing i, so you could end up with a buffer overrun if someone types more than 100 characters without pressing return -the for loop in the first implementation will only count to 98 - for(int i=0; i<100; i++) will loop with the iterator in the range [0;99] I'd probably do it something like this: http://pastebin.com/McJW1ZN2 Out of this, you'll get a variable array of termination characters (in this case, I've added the Escape key as one of them), and the resulting string will end up null terminated, so you can printf it or perform or other string operations.
Btw what does the program actually do?
It reads a string of characters from stdin until either enter is pressed, EOF is received (for example when a file is piped into stdin), or the buffer limit is reached.
that's it? O.o
There are shorter ways of doing it (you could do it in a single line of code, to be sure), but there's something to be said for readability and maintainability ;)
It was just an exercise not to use && and || part of K&R book. Fun I guess.
opiesche: why do you skip the inner loop if c == 0?
I recognize its the nil string ie end of line right? I just don't understand the use here.
They really need an edit button. It's late. I meant char and end of string :P
I guess you wouldn't really have to terminate the inner loop at c==0, but it only happens when one of the terminating characters was found, which means end of input - so we can stop iterating. Practically, it only makes a difference for large amounts of different terminating characters ;) We then insert the 0 at the end of the string - it's called null termination. In C, all string functions recognize the end of a string by a byte with the value 0 (null byte). Remember, text strings in C are just arrays of bytes with no defined length - the null byte signifies the end of the string.
I thought it was nil for strings and null for pointers :P small discretion since they are usually the same value anyway 0. The thing is that yout algorithm doesn't terminate if a 0 is found in the input, it will continue on. No? Sorry for being a smart retrice I'll be around :P
no, it doesn't tminate on 0 - I figured since input comes from stdin (normally either keyboard input or a text file), 0 would be a rare end marker to find in the input ;) you are right, however - it might be good to use 0 as another termination character, and instead mark the end of the termination list with 0xFF, for example!
IMO, your first implementation preceded by a comment describing the conditions in plain language would be the better implementation.
Join our real-time social learning platform and learn together with your friends!