Thread: code for swapping name and surname

  1. #46
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    It is harder than it seems like it should be.
    Quzah.
    I think that depends on where the OP is in the course, don't you think? This can be done in around 25 lines of code or so.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  2. #47
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    I think that depends on where the OP is in the course, don't you think? This can be done in around 25 lines of code or so.
    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:
    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;
    }
    Super simple. (Which is why I said reversing the whole array and then reversing each word was way too complicated.)

    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.

  3. #48
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    I hate to admit that I spent far too much time on this.
    That's alright, I'm sure the OP is spending a lot of time on it too.

    Quote Originally Posted by quzah View Post
    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.
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #49
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    That's alright, I'm sure the OP is spending a lot of time on it too.
    Well I figured at least one of us should be.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #50
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    LMAO.....brilliant!
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #51
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,533
    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.

  7. #52
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kermit View Post
    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.
    Yeah... I had the same thought... worked it up in about 20 lines...

  8. #53
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by kermit View Post
    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:

    Quote Originally Posted by Animesh Gaitond View Post
    Write a program to take input from the user in the following format,
    FIRSTNAME LASTNAME. Then swap the names such that the last name occurs first and the first name comes later i.e. LASTNAME FIRSTNAME
    Note:
    Do the same without using any extra array and without changing the default array size. Do not use any in-built functions(string functions).
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #54
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,602
    <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.

  10. #55
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    [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.

  11. #56
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Haha, you guys made me curious; I think I figured out the 3 line cheater solution too.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #57
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    Haha, you guys made me curious; I think I figured out the 3 line cheater solution 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. swapping help
    By mouse666666 in forum C Programming
    Replies: 3
    Last Post: 03-24-2010, 10:38 AM
  2. sort linked list by surname
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 10-02-2005, 04:06 AM
  3. Printing my initials and surname
    By Guti14 in forum C++ Programming
    Replies: 2
    Last Post: 08-20-2003, 02:46 AM
  4. swapping
    By metallicaau in forum C Programming
    Replies: 2
    Last Post: 10-08-2002, 08:17 AM
  5. swapping code
    By riley03 in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 08:06 AM