Skip to content

Instantly share code, notes, and snippets.

@ahmadmustafaanis
Created June 24, 2020 06:18
Show Gist options
  • Save ahmadmustafaanis/545a1334f9ae15a3dcb2508af07200c4 to your computer and use it in GitHub Desktop.
Save ahmadmustafaanis/545a1334f9ae15a3dcb2508af07200c4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <stack>
using namespace std;
class InduvivualWordsReverser {
private:
string h1;
stack < char > s1;
public:
InduvivualWordsReverser() {
h1 = "";
}
void init(string);
string reverseInduviuval();
};
void InduvivualWordsReverser::init(string a1)
{
h1 = a1;
}
string InduvivualWordsReverser::reverseInduviuval()
{
string reversedString = "";
for (int i = 0; i < h1.length(); i++)
{
if (h1[i] == ' ')
{
while (!(s1.empty()))
{
reversedString += s1.top();
s1.pop();
}
reversedString += ' ';
}
else
{
s1.push(h1[i]);
}
}
while (!(s1.empty()))
{
reversedString += s1.top();
s1.pop();
}
reversedString += ' ';
return reversedString;
}
int main()
{
InduvivualWordsReverser h1;
h1.init("Geeks for Geeks");
string rev = h1.reverseInduviuval();
cout << rev;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment