برنامه ای برای برعکس کردن یا معکوس یک رشته(جمله) به زبان سی پلاس پلاس به روش بازگشتی
جمعه, ۱۶ مهر ۱۳۹۵، ۰۵:۱۸ ق.ظ
gameover.blog.ir
این برنامه اصلا بهینه نیست و توصیه نمیشه(به خاطر روش بازگشتی و سرریز بافر و حافظه ی پشته یا stack)
/* Example to reverse a sentence entered by user. */
#include <iostream>
#include <string>
using namespace std;
void reverse(const string& a);
int main()
{
string a;
cout << " Please enter a string " << endl;
getline(cin, a);
reverse(a);
return 0;
}
void reverse(const string& a)
{
size_t n = a.size();
if(n == 1)
cout << a << endl;
else
{
cout << a[n-1];
string b = a.substr(0, n-1);
reverse(b);
}
}
Output
Enter a sentence: margorp emosewa awesome program
۹۵/۰۷/۱۶