Advertisement
Guest User

Recursion

a guest
May 25th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. void sign(int, ofstream&);
  9. void reverseString(const string&, ofstream&);
  10.  
  11. int main()
  12. {
  13. int s;
  14. string k;
  15. ofstream outfile("c:\\USERS\\STUDENT\\Desktop\\outfile.txt");
  16.  
  17. cout << "Enter an integer" << endl;
  18. cin >> s;
  19. sign(s, outfile);
  20.  
  21. outfile<< endl;
  22.  
  23. cout << "Enter a string" << endl;
  24. cin >> k;
  25. reverseString(k, outfile);
  26.  
  27.  
  28. return 0;
  29. }
  30.  
  31.  
  32. void sign(int n, ofstream &outfile)
  33. {
  34.  
  35. if (n==0)
  36. {
  37. return;
  38. }
  39. else
  40. {
  41. outfile << "No Parking" << endl;
  42. sign(--n, outfile);
  43. return;
  44. }
  45.  
  46. }
  47.  
  48. void reverseString(const string& str, ofstream &outfile)
  49. {
  50. int n = str.size();
  51. if(n == 1)
  52. {
  53. outfile << str << endl;
  54. return;
  55. }
  56.  
  57. else
  58. {
  59. outfile << str[n-1];
  60. string temp = str.substr(0, --n);
  61. reverseString(temp, outfile);
  62. return;
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement