Thread: Swapping surname......cont'd

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949

    Swapping surname......cont'd

    Alright so about a week ago, we had a thread which asked about how to swap surname blahblahblah. Anyway the requirements were basically that you can't use the string library, nor create another array.

    Well, we all had fun with this one on the C board, coming up with the shortest solution, ect. Anyway, I am up late, bored, not quite sober and decided to implement a generalized solution for a variable length input preserving whitespace formatting. Below is what I came up with; the problem is that this just doesn't feel like an "elegant" solution to me. Any pointers will be appreciated, thanks.
    Code:
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    #include <vector>
    
    
    int main(){
    	//Preserve whitespace formatting
    	std::cin >> std::noskipws;
    	
    	//grab entire input buffer and store in vector
    	std::istream_iterator<char>start(std::cin);
    	std::istream_iterator<char>eos;
    	std::vector<char>input(start,eos);
    
    	//reverse vector
    	std::reverse(input.begin(),input.end());
    	
    	//reverse "words" in vector based on whitespace
    	std::vector<char>::iterator wordstart=input.begin()+1,
                                        spacepos=input.begin()+1;
    	while(spacepos != input.end()){
    		if(*spacepos==' '&&(*(spacepos-1)!=' ' || *(spacepos-1)!='\n'))
                    {
    			std::reverse(wordstart,spacepos);
    			wordstart = spacepos+1;
    		}
    		++spacepos;
    	}
    	
    	//output reversed input
    	std::copy(input.begin(),
                      input.end(),
                      std::ostream_iterator<char>(std::cout));
    
    	return (0);
    }
    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. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Are the names supposed to stay reversed?


    Anyway, you have a bug related to not fully reversing the last word.

    Soma

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by phantomotap View Post
    O_o

    Are the names supposed to stay reversed?
    Yes, in fact I was working on a generalized solution regardless of whitespace formatting and number of words entered.


    Quote Originally Posted by phantomotap View Post
    Anyway, you have a bug related to not fully reversing the last word.

    Soma
    Good catch, thanks. I forgot a line of code for the last word. Here is the updated solution. Still doesn't feel "right" to me. I do understand this is one of those "why would you do it" type of questions but it is more of a brainteaser kind of thing. Any additional comments, as always, are appreciated.
    Code:
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    #include <vector>
    
    
    int main(){
    	//Preserve whitespace formatting
    	std::cin >> std::noskipws;
    	
    	//grab entire input buffer and store in vector
    	std::istream_iterator<char>start(std::cin);
    	std::istream_iterator<char>eos;
    	std::vector<char>input(start,eos);
    
    	//reverse vector
    	std::reverse(input.begin(),input.end());
    	
    	//reverse "words"
    	std::vector<char>::iterator wordstart=input.begin()+1,spacepos=input.begin()+1;
    	while(spacepos != input.end()){
    		if(*spacepos==' '&&(*(spacepos-1)!=' ' || *(spacepos-1)!='\n')){
    			std::reverse(wordstart,spacepos);
    			wordstart = spacepos+1;
    		}
    		++spacepos;
    	}
    	//reverse last word
    	std::reverse(wordstart,spacepos);
    	
    	//output reversed input
    	std::copy(input.begin(),input.end(),std::ostream_iterator<char>(std::cout));
    
    	return (0);
    }
    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. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Might want to search the board: word reverser, not every idea is original.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by whiteflags View Post
    Might want to search the board: word reverser, not every idea is original.
    I understand that, and thankyou for the advice. However, I was more or less trying to work within the confines of not using any string functions. I have coded a solution in C already that accomplishes this, as well as the solution above (the second one). I was just looking for some possible ideas as this one didn't seem elegant to me and I am more than aware that I am not familiar with every aspect of the STL.
    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. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think you can take C code and make it C++ code just by using strings, string iterators, and calling reverse. The actual code changes very little.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    In that case, consider the "STL" functions `find_first_of' and friends.

    And you still have a couple of bugs related to the last word.

    Soma

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by whiteflags View Post
    I think you can take C code and make it C++ code just by using strings, string iterators, and calling reverse. The actual code changes very little.
    Oh, I see. My initial assumption for the problem is that no string functions can be used. I think you may have missed that.

    Quote Originally Posted by phantomotap View Post
    O_o

    In that case, consider the "STL" functions `find_first_of' and friends.

    And you still have a couple of bugs related to the last word.

    Soma
    I will take a look into that, thank you. Also for the bug, are you referring to how it treats the newline for multiple line input?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code for swapping name and surname
    By Animesh Gaitond in forum C Programming
    Replies: 56
    Last Post: 08-15-2011, 03:15 PM
  2. sort linked list by surname
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 10-02-2005, 04:06 AM
  3. cont. Help...
    By P3st in forum C++ Programming
    Replies: 0
    Last Post: 11-16-2003, 03:42 PM
  4. Printing my initials and surname
    By Guti14 in forum C++ Programming
    Replies: 2
    Last Post: 08-20-2003, 02:46 AM
  5. cont...
    By Sarah in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2001, 12:28 PM