Review: small recursive code

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 448
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Review: small recursive code

 
0
  #1
Aug 8th, 2008
Hi Guys,

i just wrote a program to print a sentence backwards using recursion. But i'm not too happy about it, if someone can give me a more optimized solution i'll be glad. You have to use std::string class.

input:
where the streets have no name
output:
name no have streets the where

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. // read a sentence and print it backwards using recursion
  7.  
  8. std::string mySentence(std::string);
  9.  
  10. int main()
  11. {
  12. std::string query;
  13. std::cout << "enter a sentence" << std::endl;
  14.  
  15. if ( std::getline ( std::cin,query ) )
  16. std::cout << mySentence(query) << std::endl;
  17. }
  18.  
  19. std::string
  20. mySentence(std::string sentence)
  21. {
  22. std::stringstream ss(sentence);
  23. std::string token;
  24. int wordCount = 0;
  25. while(ss>>token)
  26. {
  27. wordCount++;
  28. }
  29.  
  30. if(wordCount == 1)
  31. return sentence;
  32.  
  33. int count = 0;
  34. std::string::iterator it;
  35. it = sentence.end();
  36. while(*it != ' ')
  37. {
  38. it--;
  39. count++;
  40. }
  41. int len = sentence.length();
  42. int pos = len - count;
  43. std::string subString = sentence.substr(0,pos);
  44. return sentence.substr(pos) + mySentence(subString);
  45. }
Last edited by Agni; Aug 8th, 2008 at 9:29 am.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Review: small recursive code

 
1
  #2
Aug 8th, 2008
Try to compress:
  1. bool Reverser(std::istream& sentence = std::cin, bool im1st = true)
  2. {
  3. std::string word;
  4. if (sentence >> word)
  5. {
  6. if (Reverser(sentence,false))
  7. std::cout << " ";
  8. std::cout << word;
  9. if (im1st)
  10. std::cout << std::endl;
  11. return true;
  12. }
  13. return false;
  14. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 266
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Review: small recursive code

 
1
  #3
Aug 8th, 2008
You might try the following untested logic:
  1. declare mySentence() to:
  2. 1) have return type void
  3. 2) receive a reference to a stringstream
  4. 3) have a local string variable called token
  5. 4) if you can extract a string from the stringstream into token
  6. a) recursively call mySentence again passing it the
  7. stringstream which is now advanced 1 word from where it was
  8. b) output token after return from the recursive call
  9.  
  10. in main()
  11. 1) receive user input into a string
  12. 2) declare and initialize a stringstream with user input
  13. 3) call mySentence passing it the stringstream

Edit: Beaten by ArkM who is using similar logic. Here's the actual code I had written
  1. void mySentence(std::stringstream & ss)
  2. {
  3. std::string token;
  4. if(ss >> token)
  5. {
  6. mySentence(ss);
  7. std::cout << token << std::endl;
  8. }
  9. }
Last edited by Lerner; Aug 8th, 2008 at 11:01 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Review: small recursive code

 
0
  #4
Aug 8th, 2008
Yes, it's the same (obvious) recursive pattern.
Regrettably, I was forced to add some bells and twistles to separate words with blanks.
It seems there is some compression reserve here...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Review: small recursive code

 
1
  #5
Aug 8th, 2008
to reproduce the white spaces in the original in the reversed sentence:
  1. #include <string>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <iostream>
  5. #include <ctype.h>
  6.  
  7. template< typename REV_CHAR_ITER >
  8. void print_reverse( REV_CHAR_ITER rbeg, REV_CHAR_ITER rend )
  9. {
  10. REV_CHAR_ITER found = std::find_if( rbeg, rend, ::isspace ) ;
  11. std::copy( found.base(), rbeg.base(),
  12. std::ostream_iterator<char>(std::cout) ) ;
  13.  
  14. if( found != rend )
  15. {
  16. std::cout << *found ;
  17. print_reverse( ++found, rend ) ;
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. const std::string sentence = "test sentence, containing "
  24. "a few extra spaces,\t\ttabs" ;
  25. print_reverse( sentence.rbegin(), sentence.rend() ) ;
  26. std::cout << '\n' << sentence << '\n' ;
  27. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Review: small recursive code

 
0
  #6
Aug 8th, 2008
Yet another variant(s):
  1. void Reverser(std::istream& sentence = std::cin, char delim = '\n')
  2. {
  3. std::string word;
  4. if (sentence >> word)
  5. {
  6. Reverser(sentence,' ');
  7. cout << word << delim;
  8. }
  9. }
  10. inline
  11. void Reverser(const std::string& sentence)
  12. {
  13. Reverser(std::istringstream(sentence));
  14. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

Re: Review: small recursive code

 
1
  #7
Aug 8th, 2008
Depending on the spacing...(if it doesn't matter) you can use a LIFO stack... I'd use the STL stack...the code would look like this:
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4.  
  5. using namespace std;
  6.  
  7. stack<string> sstack;
  8. string word;
  9.  
  10. int main( void )
  11. {
  12. while( cin >> word ) sstack.push( word );
  13. while( !sstack.empty() ) {
  14. cout << sstack.top() << " ";
  15. sstack.pop();
  16. return 0;
  17. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 448
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Review: small recursive code

 
0
  #8
Aug 9th, 2008
Thanks a lot guys ... all these were excellent solutions... i'm trying to condition my 'approach' to solving problems ....
thanks
-chandra
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC