I hate to admit that I spent far too much time on this. The solution I have now is about ten lines or so. (It's 15 the way I format, but if you didn't put each braces on its own line, it would be less. Actual working lines are around ten.)
It's probably not the optimum efficiency, but it is absolutely the simplest thing I could think of. I was really over complicating this, and that's why I spent so much time. I couldn't wrap my head around a solution - and I didn't want to go read about one and then try to implement that, I wanted to come up with one on my own. I had a few really close solutions, but kept hitting minor snags with them that took far too much mental effort on my part to try to sort out.
I'll post it here in a week or so if I can remember to do so. Before I read their actual homework requirements (who reads that?! It's not my homework!), I had a very simple:Super simple. (Which is why I said reversing the whole array and then reversing each word was way too complicated.)Code:int sscspswap( char buf[] ) { char first[ BUFSIZ ] = {0}, last[ BUFSIZ ] = {0}; if( sscanf( buf, "%s %s", first, last ) == 2 ) { sprintf( buf, "%s %s", last, first ); return 0; } printf( "buf not formatted correctly.\nYou lose!\n" ); return -1; }
After I read the requirements, I was also going to cheat the rules (and I may still, just because I like cheating requirements) and use the stack (recursion) to hold my strings while I swap them around. I still might, because technically (and I'm all about cheating technicality in these types of problems) the stack isn't "using another array".
Quzah.
Hope is the first step on the road to disappointment.
That's alright, I'm sure the OP is spending a lot of time on it too.
I wouldn't think that would be cheating at all. They already took away the standard string library, I wouldn't see why they would take away the stack, however I suppose depending on the OP's country they may pretend the stack doesn't exist.
I came up with a generalized solution to this one, that way whether it was 2 words or max input it would work the same. (Check your PM). I agree with you, we should revisit this one in a week and post actual solutions. Or perhaps we could start a new contest for all the newbs in the contest board with this question and then post all the solutions at the end of the month or something.
I like the cheating idea better. Since the requirements, as far as I can tell, only seems to be interested in the final output, it is simple enough to run through the string, find the space, change that to a null, print what follows it, then print the first part. I would post what I was playing around with, but it is not interesting, and might be tempting for the OP.
I am always one for cheating but I don't think that actually meets the requirements:
The example it gives just shows the names being swapped but the way I read the actual requirements it doesn't appear that would work since you couldn't just use a single printf() statement to print the string. Anway, that's the way I read it.
<tease>
The "interesting" part of my "print only" cheat answer is only 3 lines.
</tease>
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
[Name That Tune]
I can do that in one line, Tom!
[/Name That Tune]
As above, print only, not modifying original input. I bet I have Salem's three-line method, too.
If all we are doing is printing, one line for variables, one for the work, one for the print, if you don't count wrapping it nicely in a function as lines.
edit - more cheating:
1. do the above, except the printing
2. fprintf to a file
3. fscanf to your original buffer
Quzah.
Last edited by quzah; 08-15-2011 at 03:58 PM.
Hope is the first step on the road to disappointment.