943,566 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2203
  • C++ RSS
Aug 8th, 2008
0

Review: small recursive code

Expand Post »
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

c++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Aug 8th, 2008
1

Re: Review: small recursive code

Try to compress:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 8th, 2008
1

Re: Review: small recursive code

You might try the following untested logic:
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Aug 8th, 2008
0

Re: Review: small recursive code

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 8th, 2008
1

Re: Review: small recursive code

to reproduce the white spaces in the original in the reversed sentence:
c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Aug 8th, 2008
0

Re: Review: small recursive code

Yet another variant(s):
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 8th, 2008
1

Re: Review: small recursive code

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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 12
Solved Threads: 5
Junior Poster in Training
gregorynoob is offline Offline
86 posts
since Jun 2008
Aug 9th, 2008
0

Re: Review: small recursive code

Thanks a lot guys ... all these were excellent solutions... i'm trying to condition my 'approach' to solving problems ....
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ member access question
Next Thread in C++ Forum Timeline: How to make sure no numbers are the same?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC